Created
April 10, 2012 16:14
-
-
Save yoko/2352527 to your computer and use it in GitHub Desktop.
Tumblr to Evernote
This file contains hidden or 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
on growl(str, image_file) | |
if image_file is false then | |
set icon to "-a 'Tumblr Backup'" | |
else | |
set icon to "--image " & quoted form of (POSIX path of (image_file as text)) | |
end if | |
try | |
do shell script "/usr/local/bin/growlnotify " & icon & " -t \"Tumblr to Evernote\" -m " & quoted form of str | |
end try | |
end growl | |
on test(pattern, str) | |
try | |
(do shell script "ruby -e \"puts " & pattern & " =~ " & quoted form of str & "\"") as integer | |
return true | |
on error | |
return false | |
end try | |
end test | |
on find_note(intitle) | |
tell application "Evernote" | |
set existing_notes to find notes "notebook:Tumblr intitle:" & intitle | |
if length of existing_notes is 0 then | |
return false | |
else | |
return existing_notes | |
end if | |
end tell | |
end find_note | |
on set_green_label(image_file) | |
tell application "Finder" | |
set label index of image_file to 6 | |
end tell | |
end set_green_label | |
on run {input, parameters} | |
if length of input is 0 then | |
growl("Image not found", false) | |
return false | |
end if | |
tell application "Evernote" | |
if (not (notebook named "Tumblr" exists)) then | |
make notebook with properties {name:"Tumblr"} | |
end if | |
end tell | |
repeat with image_file in input | |
tell application "Finder" | |
set file_name to get name of item image_file | |
end tell | |
set text item delimiters of AppleScript to "_" | |
set name_count to count text item of file_name | |
if name_count is 1 then | |
-- 123456.jpg | |
set text item delimiters of AppleScript to "." | |
set post_id to first text item of file_name | |
if find_note(post_id) is not false then | |
growl("Already exists", false) | |
else if test("/^[0-9]+$/", post_id) then | |
tell application "Evernote" | |
set new_note to create note title post_id from file image_file notebook "Tumblr" | |
tell new_note to set source URL to "http://yksk.tumblr.com/post/" & post_id | |
end tell | |
growl("Added", image_file) | |
set_green_label(image_file) | |
end if | |
else if name_count is 2 then | |
-- user_123456.jpg | |
set user to first text item of file_name | |
set last_item to last text item of file_name | |
set text item delimiters of AppleScript to "." | |
set post_id to first text item of last_item | |
tell application "Evernote" | |
set new_note to create note title post_id from file image_file notebook "Tumblr" | |
tell new_note to set source URL to "http://" & user & ".tumblr.com/post/" & post_id | |
end tell | |
growl("Added", image_file) | |
set_green_label(image_file) | |
else if name_count is 3 then | |
-- user_123456_[0..].jpg | |
set user to first text item of file_name | |
set post_id to second text item of file_name | |
set last_item to last text item of file_name | |
set text item delimiters of AppleScript to "." | |
set image_count to first text item of last_item | |
if image_count is "0" then | |
tell application "Evernote" | |
set new_note to create note title post_id from file image_file notebook "Tumblr" | |
tell new_note to set source URL to "http://" & user & ".tumblr.com/post/" & post_id | |
end tell | |
growl("Added", image_file) | |
set_green_label(image_file) | |
else | |
set existing_notes to find_note(post_id) | |
if existing_notes is not false then | |
set post_note to first item of existing_notes | |
tell application "Evernote" | |
tell post_note to append attachment image_file | |
end tell | |
growl("Added", image_file) | |
set_green_label(image_file) | |
end if | |
end if | |
end if | |
end repeat | |
growl("Completed", false) | |
return input | |
end run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment