Last active
November 6, 2021 20:08
-
-
Save patrickgilmour/31af066b283d70049e4ea7de68c0444d to your computer and use it in GitHub Desktop.
AppleScript to import Ulysses text files to DayOne
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--Script to import text files (Markdown) | |
--exported from External folder in Ulysses (Mac) | |
--to a Finder folder THE_FOLDER | |
--Imports entries from that folder with tags (Keywords) and removes tags | |
--from body of the entry | |
--Entry date is same as Creation Date of text file | |
--Required DayOne CLI tool installed on Mac | |
tell application "Finder" | |
set listOfFiles to contents of folder "Macintosh HD:Users:pat:Desktop:THE_FOLDER" | |
repeat with i from 1 to count of listOfFiles | |
--Get List of Files | |
set thisFile to item i of listOfFiles | |
set thisFileAlias to thisFile as alias | |
--Get creation date | |
set creationDate to (creation date of (get info for thisFileAlias)) | |
--log creationDate | |
--Format Creation Date | |
set {year:y, month:m, day:d} to creationDate | |
set timeStr to time string of creationDate | |
tell (y * 10000 + m * 100 + d) as string to text 1 thru 4 & "-" & text 5 thru 6 & "-" & text 7 thru 8 & " " & timeStr | |
set dateTimeStr to the result | |
--log dateTimeStr | |
--Get file contents | |
set thisFile to thisFile as string | |
set contentsOfFile to read file thisFile as «class utf8» | |
set contentsOfFile to contentsOfFile as string | |
--Escape and Replace Problem Characters in File Contents String | |
set escapeChar to character id 92 | |
set doubleQuoteChar to character id 34 | |
set escapeStr1 to escapeChar & "'" | |
set escapeStr2 to "\"" | |
--Replace 3 things... | |
set contentsOfFile to my replaceText("'", escapeStr1, contentsOfFile) | |
set contentsOfFile to my replaceText(doubleQuoteChar, escapeChar & escapeStr2, contentsOfFile) | |
set char1 to characters 1 thru 1 of result as string | |
if (char1 = "-") then | |
set contentsOfFile to "Untitled | |
" & contentsOfFile | |
--log contentsOfFile | |
end if | |
--Define tags | |
--Create grep search command | |
set thecommandstring to "echo \"" & contentsOfFile & "\"|grep -Eo '(#[[:alpha:]-]+)([0-9]?)'" | |
--log thecommandstring | |
--Defaults | |
set tags to "Imported" | |
set grepResult to "" | |
--Execute Command | |
try | |
set grepResult to do shell script thecommandstring as string | |
end try | |
--log "grepResult: " & grepResult | |
--Remove Hashtags and Line Returns from Tag List | |
--Line return id is 13 | |
set returnChar to character id 13 | |
set parsedGrepResult to my replaceText("#", "", grepResult) | |
set parsedGrepResult to my replaceText(returnChar, " ", parsedGrepResult) | |
--log "parsedGrepResult " & parsedGrepResult | |
--Set tags to string | |
set tags to tags & " " & parsedGrepResult as string | |
--log "Tags: " & tags | |
--Remove Hashtag Keywords from BODY Text | |
--Create GREP -v exclude command, where -v returns only what was *not* matched | |
set thecommandstring to "echo \"" & contentsOfFile & "\"|LC_CTYPE=en_US.UTF-8 grep -Ev '([#])([[:alpha:]-]+)([0-9])?'" | |
--log contentsOfFile | |
--Execute Command to remove tags from BODY | |
try | |
set contentsOfFile to do shell script thecommandstring | |
end try | |
--Escape characters may have been parsed out by grep so need to be reapplied | |
set contentsOfFile to my replaceText("'", escapeStr1, contentsOfFile) | |
set contentsOfFile to my replaceText(doubleQuoteChar, escapeChar & escapeStr2, contentsOfFile) | |
--Write Day One entry | |
set command to "/usr/local/bin/dayone2 -t " & tags & " -j Import -d='" & dateTimeStr & "' -z='America/New_York' new " & "\"" & contentsOfFile & "\"" | |
--log command | |
do shell script (command) | |
log "Written item: " & i | |
end repeat | |
end tell | |
--AppleScript string replace handler | |
on replaceText(find, replace, subject) | |
set prevTIDs to text item delimiters of AppleScript | |
set text item delimiters of AppleScript to find | |
set subject to text items of subject | |
set text item delimiters of AppleScript to replace | |
set subject to "" & subject | |
set text item delimiters of AppleScript to prevTIDs | |
return subject | |
end replaceText |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment