Skip to content

Instantly share code, notes, and snippets.

@kerotaa
Created June 17, 2013 17:07
Show Gist options
  • Save kerotaa/5798444 to your computer and use it in GitHub Desktop.
Save kerotaa/5798444 to your computer and use it in GitHub Desktop.
-- Settings
set timezone to "+09:00"
set postdir to "Macintosh HD:Users:kerotaa:Sites:kerotaa.github.com:_posts"
set editor to "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl"
-- Get Date
set dt to (current date)
on date_to_iso(dt)
set {year:y, month:m, day:d} to dt
set y to text 2 through -1 of ((y + 10000) as text)
set m to text 2 through -1 of ((m + 100) as text)
set d to text 2 through -1 of ((d + 100) as text)
return y & "-" & m & "-" & d
end date_to_iso
on date_time_to_iso(dt, timezone)
set {year:y, month:m, day:d, hours:h, minutes:min, seconds:s} to dt
set y to text 2 through -1 of ((y + 10000) as text)
set m to text 2 through -1 of ((m + 100) as text)
set d to text 2 through -1 of ((d + 100) as text)
set h to text 2 through -1 of ((h + 100) as text)
set min to text 2 through -1 of ((min + 100) as text)
set s to text 2 through -1 of ((s + 100) as text)
return y & "-" & m & "-" & d & "T" & h & ":" & min & ":" & s & timezone
end date_time_to_iso
-- Replacement
on replaceText(theText, serchStr, replaceStr)
set tmp to AppleScript's text item delimiters
set AppleScript's text item delimiters to serchStr
set theList to every text item of theText
set AppleScript's text item delimiters to replaceStr
set theText to theList as string
set AppleScript's text item delimiters to tmp
return theText
end replaceText
-- Make file
on create_post_file(postdir, filename)
tell application "Finder" to make new file with properties {name:filename} at alias postdir
end create_post_file
-- Write file
on write_post_file(postdir, filename, iso_datetime)
set filepath to {postdir & ":" & filename} as text
set input_text to {"---
title:
layout: post
date: " & iso_datetime & "
categories: []
tags: []
---
"} as text
try
set fn to open for access file filepath with write permission
set eof of fn to 0
write input_text to fn
close access fn
end try
end write_post_file
-- Open file with editor
on open_file_with_editor(postdir, filename, editor)
set filepath to {postdir & ":" & filename} as text
set filepath_std to replaceText(replaceText(filepath, ":", "/"), "Macintosh HD", "")
do shell script {"'" & editor & "' '" & filepath_std & "'"} as text
end open_file_with_editor
-- Main
display dialog "Input post slug :" default answer ""
set slug to text returned of result
set iso_date to date_to_iso(dt)
set iso_datetime to date_time_to_iso(dt, timezone)
set filename to {iso_date & "-" & slug & ".md"} as text
create_post_file(postdir, filename)
write_post_file(postdir, filename, iso_datetime)
open_file_with_editor(postdir, filename, editor)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment