Tuesday, December 18, 2012

Applescript to make DayOne entries from a folder with images

Today, I made an Applescript to make DayOne entries from a folder with images. It uses EXIFtool to extract date and time from the meta information stored inside the image. The code was heavily borrowed from this posting on making DayOne entries from Aperture. Somehow, that script did not work for some images.
If you want the image file to be deleted, uncomment the line at the end of the script

## start of code ##

# Create Dayone entries from folder with JPEGs
# based on http://d.pr/n/a2MQ

# Can only be used for http://dayoneapp.com
# in combination with the associated CLI tools
 
# change the path to your export folder
set a_Folder to "/Users/username/Desktop/export/"

set a_folder_list to list folder a_Folder without invisibles
repeat with x from 1 to count of a_folder_list
    set the_file to quoted form of (a_Folder & item x of a_folder_list)
    set shell_command to "exiftool -exif:DateTimeOriginal " & the_file
    #display dialog shell_command
    set a_date to do shell script shell_command
    set theYear to word 4 of a_date as integer
    set theMonth to word 5 of a_date
    set theDay to word 6 of a_date
    set theHour to word 7 of a_date
    set theMin to word 8 of a_date
    set theSec to word 9 of a_date
    set theDate to theMonth & "/" & theDay & "/" & theYear as string
    #display dialog theMin
    set theAP to "AM"
    if theMin is less than 10 then
        set theMin to "0" & theMin
    end if
    if theHour is greater than 12 then
        set theAP to "PM"
        set theHour to theHour - 12
    end if
    set theTime to theHour & ":" & theMin & " " & theAP as string
    set theDateTime to "\"" & theDate & " " & theTime & "\""
   
    set theScript to "echo \"\" | /usr/local/bin/dayone -p=" & the_File & " -d=" & theDateTime & " new" as string
   
    set theEntry to do shell script theScript
    set theLength to the length of theEntry
    set theStart to theLength - 39
    set theEnd to theLength - 8
    set theUDID to text theStart thru theEnd of theEntry
   
    tell application "Safari"
        set the URL of the front document to "dayone://edit?entryId=" & theUDID
    end tell


#    uncomment the line below to enable deletion of the image file
#    do shell script "rm " & the_File
   
end repeat


## end of code ##