Create a gist now

Instantly share code, notes, and snippets.

Embed
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
--Convert an Instapaper note file from IFTTT to the FIRST note in GRAV
set theFile to (choose file with prompt "Select a file to read:" of type {"txt"})
--Read the file as UTF-8
open for access theFile
set thePath to the POSIX path of theFile
set fileContents to (do shell script "cat " & quoted form of thePath)
set Mytitle to paragraph 1 of fileContents
set Mytext to paragraph 2 of fileContents
set Myurl to paragraph 3 of fileContents
set Mytime to paragraph 4 of fileContents
close access theFile
--Clean up the time
set timeStr to Mytime
set myday to word 2 of timeStr
--set mydate to word 2 of timeStr
set mymonth to word 1 of timeStr
set myyear to word 3 of timeStr
set myhour to word 5 of timeStr
set mymins to character 3 of word 6 of timeStr
-- change month name to month number as string with a loop
set possibleMonths to {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
repeat with i from 1 to 12
if (item i of possibleMonths) is mymonth then
set monthno to i as string
end if
end repeat
set monthno to my padNumber(monthno)
set myday to my padNumber(myday)
if mymins is "P" then
set newtime to (myhour as integer) + 12
set myhour to newtime as string
end if
--construct timestamp
set timestamp to "'" & myday & "-" & monthno & "-" & myyear & " " & myhour & ":00'"
set theyaml to "/Applications/MAMP/htdocs/grav-admin/user/pages/04.reviews/highlight-yaml.txt"
set yaml to (read theyaml)
set find to "–"
set replace to "--"
set someText to Mytitle
set Mytitle to my replaceText(find, replace, someText)
set find to "|"
set replace to "--"
set someText to Mytitle
set Mytitle to my replaceText(find, replace, someText)
set find to "title: 'TITLE'"
set replace to "title: '<i class=\"fa fa-sticky-note\"></i> " & Mytitle & "'"
set someText to yaml
set yaml to my replaceText(find, replace, someText)
set find to "date: 'DD-MM-YYYY HH:MM'"
set replace to "date: " & timestamp
set someText to yaml
set yaml to my replaceText(find, replace, someText)
--construct post body
set Myoutput to "<p class=\"highlight\">" & Mytext & "</p>" & "
<p>Note from <a href=\"" & Myurl & "\" class=\"styling u-bookmark-of\">" & Mytitle & "</a></p>
"
set yaml to yaml & Myoutput
--make new folder
set foldername to Mytitle
set find to " "
set replace to "-"
set someText to foldername
set foldername to my replaceText(find, replace, someText)
set find to "&"
set replace to ""
set someText to foldername
set foldername to my replaceText(find, replace, someText)
set find to "@"
set replace to ""
set someText to foldername
set foldername to my replaceText(find, replace, someText)
set find to ":"
set replace to ""
set someText to foldername
set foldername to my replaceText(find, replace, someText)
set foldername to do shell script "echo " & quoted form of (foldername) & " | tr A-Z a-z"
set myPath to POSIX path of ("/Applications/MAMP/htdocs/grav-admin/user/pages/04.reviews" as text) & "/" & foldername
do shell script "mkdir -m 777 " & myPath
--write content out to file
set textFile to myPath & "/item.md"
do shell script "echo " & quoted form of yaml & " > " & quoted form of textFile
display dialog yaml
on replaceText(find, replace, someText)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set someText to text items of someText
set text item delimiters of AppleScript to replace
set someText to "" & someText
set text item delimiters of AppleScript to prevTIDs
return someText
end replaceText
on padNumber(theNumber)
if number of characters in theNumber is 1 then
set theNumber to "0" & theNumber as string
end if
return theNumber
end padNumber
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment