Skip to content

Instantly share code, notes, and snippets.

@t413
Created June 24, 2012 06:14
Show Gist options
  • Save t413/2981889 to your computer and use it in GitHub Desktop.
Save t413/2981889 to your computer and use it in GitHub Desktop.
Aperture SFTP Exporter AppleScript
--
-- Aperture SFTP Exporter --
-- by Tim O'Brien, t413.com
--
-- -uses selected images in Aperture
-- -exports images to a subdirectory (asks each time, default is based on project name)
-- -growl notifications of sftp upload progress
-- -speed metrics (upload speed, export speed)
--
-- To enable SFTP uploading:
-- 1. generate your keys typing this command in terminal (on PC)
-- ssh-keygen -t rsa
-- 2. upload your public key to the destination server:
-- cat ~/.ssh/id_rsa.pub | ssh USERNAME@SERVER 'cat >> .ssh/authorized_keys'
-- 3. Test automatic login: (should login without password)
-- ssh USERNAME@SERVER
property DestServer : ""
property DestDir : ""
property ExportSettingName : ""
if (DestServer = "") then DefaultsSetup()
SetupGrowl() --setup growl notifications
--get a list of the pictures to export
tell application "Aperture"
set imageSel to selection --load selected images
if ((count imageSel) is less than 1) then error {"No images selected in Aperture, select images to export."}
set projectObject to get value of other tag "MasterProject" of (item 1 of imageSel)
set albName to (get name of projectObject) --get album name of the first image
--add a keyword like '4star' to every rated photo:
repeat with im in imageSel
set tname to "" & (main rating of im) & "star" --keywords of im
tell im to make new keyword with properties {id:tname, name:tname, parents:""}
end repeat
end tell
--get web safe album name (ask user to verify)
set dialogueRes to display dialog "Web-safe album name?" & return & return & ¬
" (these are your current settings:)" & return & ¬
" Server: " & DestServer & return & ¬
" Base Directory: " & DestDir & return & ¬
" Export Preset: " & ExportSettingName & return ¬
default answer (my CleanName(albName)) buttons {"Setup", "Cancel", "OK"} default button 3
set webSafealbName to (my CleanName(text returned of dialogueRes))
if button returned of dialogueRes = "Setup" then DefaultsSetup()
--make temporary folder for the album
set tempAlbPath to (path to temporary items as string) & webSafealbName --mac:path as string
set exportPhotos to true
tell application "Finder" to if exists folder tempAlbPath then
tell application "Finder" to set numberOfFiles to count of (files in folder tempAlbPath)
set exportPhotos to ("Replace" = button returned of (display dialog "Export folder already exists." & return & return & ¬
"(" & numberOfFiles & ") Files in folder." & return ¬
buttons {"Replace", "Skip re-export"} default button 2))
if exportPhotos then
tell application "Finder" to delete tempAlbPath --trash the old temperorary file
do shell script "mkdir " & (quoted form of (POSIX path of tempAlbPath))
end if
else
do shell script "mkdir " & (quoted form of (POSIX path of tempAlbPath))
end if
if exportPhotos then
growlNotif("Starting Image Export", "Exporting " & (count imageSel) & " Images.")
set timeStart to my getTime()
--Aperture Export full size images
tell application "Aperture" to export imageSel using export setting ExportSettingName to tempAlbPath
beep
growlNotif("Image Export Finished", "Images exported: " & (count imageSel) & return & ¬
"Export time: " & (round ((my getTime()) - timeStart)) & " sec")
end if
property ConfirmBeforeUpload : true
if ConfirmBeforeUpload then
tell application "Finder" to set numberOfFiles to count of (files in folder tempAlbPath)
set ConfirmBeforeUpload to not ("Don't ask again" = button returned of (display dialog "Export Done." & return & return & ¬
"(" & numberOfFiles & ") Files to export." & return & ¬
" Server: " & DestServer & return & ¬
" To: " & DestDir & webSafealbName & return ¬
buttons {"Don't ask again", "Cancel", "OK"} default button 3))
end if
--build an alias list of the files to upload
tell application "Finder" to set uploadFiles to (files of entire contents of (tempAlbPath as alias)) as alias list
set the item_count to the number of items in uploadFiles
try --tries to make the album folder on the server (should stop the script if folder exists)
do shell script "ssh " & DestServer & " mkdir " & DestDir & webSafealbName
on error errorMessage
display dialog "Creating directory albums/" & webSafealbName & "/ FAILED." & return & return & "Error: " & errorMessage ¬
buttons {"Continue anyway", "Cancel"} default button 1 with icon 0
end try
growlNotif("Starting Upload", "Folder (" & webSafealbName & ") created, starting upload of " & item_count & " images")
--spc upload
set totalTimeStart to my getTime()
set counter to 1 --keep track of the index we're on
set totalFileSize to 0 --keep track of bytes transferre
repeat with aFile in uploadFiles
tell application "Finder" to set theFileName to aFile as text
set theFilePath to (POSIX path of aFile) as text
set theFileInfo to (info for alias theFileName)
set FileN to name of theFileInfo
set fileSize to size of theFileInfo
set totalFileSize to totalFileSize + fileSize
set timeStart to my getTime()
do shell script "scp -q " & (quoted form of theFilePath) & " " & DestServer & ":" & quoted form of (DestDir & webSafealbName & "/" & CleanName(FileN))
set dt to ((my getTime()) - timeStart)
growlNotif("Uploaded " & FileN, "File " & counter & " of " & item_count & " " & (round (fileSize / dt) / 1000) & "KB/s")
set counter to counter + 1
end repeat
set dt to ((my getTime()) - totalTimeStart)
display dialog "Uploaded Complete" & return & return & ¬
"Uploaded files: " & item_count & return & ¬
"Size: " & (round (totalFileSize / 1000 / 1000 * 10) / 10) & " MB" & return & ¬
"Speed: " & (round (totalFileSize / dt) / 1000) & "KB/s" & return & ¬
"Time: " & (round (dt)) & ¬
" seconds" buttons {"OK"} default button 1
tell application "Finder" to delete tempAlbPath --trash the temperorary file
growlNotif("rm'ed the tmp file", "")
--set DestServer to "" --this resets the property and makes the setup process run again.
-------------------------------------------------------------
on getTime()
return do shell script "perl -e 'use Time::HiRes qw(time); print time'"
end getTime
-------------------------------------------------------------
on DefaultsSetup()
set DestServer to text returned of (display dialog "-- Setup Step 1/3 -- " & return & return & ¬
"Upload server (in the form of [email protected]) " & return ¬
default answer "[email protected]")
set DestDir to text returned of (display dialog "-- Setup Step 2/3 -- " & return & return & ¬
"Base upload path: (INCLUDE TRAILING /)" default answer "/var/www/htdocs/albums/")
tell application "Aperture" to set export_setting to name of export settings
set ExportSettingName to item 1 of (choose from list export_setting with prompt "-- Setup Step 3/3 -- " & return & return & ¬
"Choose a file type preset")
end DefaultsSetup
-------------------------------------------------------------
on SetupGrowl()
tell application "GrowlHelperApp"
set the allNotificationsList to {"Upload Status"}
set the enabledNotificationsList to allNotificationsList
-- Register our script with growl.
register as application "Aperture Exporter" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "Aperture"
end tell
end SetupGrowl
on growlNotif(mytitle, desc)
tell application "GrowlHelperApp"
notify with name "Upload Status" title mytitle description desc application name "Aperture Exporter"
end tell
end growlNotif
-------------------------------------------------------------
--function for cleaning the characters from the file name
on CleanName(theName)
set disallowedChars to " :;,/|!@#$%^&*()+" --disallowedChars will be replaced with the replacementChar
set disallowedChars2 to "'" --will be removed altogether
set replacementCharacter to "_" --what to replace disallowedChars with
set newName to ""
repeat with i from 1 to length of theName
--check if the character is in disallowedChars
--replace it with the replacementCharacter if it is
if ((character i of theName) is in disallowedChars) then
set newName to newName & replacementCharacter
--check if the character is in disallowedChars2
--remove it completely if it is
else if ((character i of theName) is in disallowedChars2) then
set newName to newName & ""
--if the character is not in either disallowedChars or
--disallowedChars2, keep it in the file name
else
set newName to newName & character i of theName
end if
end repeat
return newName
end CleanName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment