Skip to content

Instantly share code, notes, and snippets.

@carlosefonseca
Created April 30, 2013 12:29
Show Gist options
  • Save carlosefonseca/5488397 to your computer and use it in GitHub Desktop.
Save carlosefonseca/5488397 to your computer and use it in GitHub Desktop.
Automator Service that receives Image files, asks for a width and resizes the image to that width, and resizes a copy of the original to the double of the entered width, saving this with a @2x sufix. Uses Acorn. Applescript appended for reading, .workflow appended for usage (open to install).
property enteredWidth : 0
property newPath : ""
on run {input, parameters}
repeat with fileAlias in input
try
display dialog "Enter width for 1x of " & (POSIX path of fileAlias) & ":" default answer ""
set dialogInfo to result
set enteredWidth to (text returned of dialogInfo as integer)
set the newPath to my replace_chars((POSIX path of fileAlias), ".png", "@2x.png")
on error dialogError
return
end try
tell application "Acorn"
set doc to open (POSIX path of fileAlias)
tell doc
trim
duplicate
resize image width enteredWidth
save
close
end tell
tell front document
resize image width (2 * enteredWidth)
save in newPath
close
end tell
end tell
end repeat
return input
end run
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>AMApplicationBuild</key>
<string>346</string>
<key>AMApplicationVersion</key>
<string>2.3</string>
<key>AMDocumentVersion</key>
<string>2</string>
<key>actions</key>
<array>
<dict>
<key>action</key>
<dict>
<key>AMAccepts</key>
<dict>
<key>Container</key>
<string>List</string>
<key>Optional</key>
<true/>
<key>Types</key>
<array>
<string>com.apple.applescript.object</string>
</array>
</dict>
<key>AMActionVersion</key>
<string>1.0.2</string>
<key>AMApplication</key>
<array>
<string>Automator</string>
</array>
<key>AMParameterProperties</key>
<dict>
<key>source</key>
<dict/>
</dict>
<key>AMProvides</key>
<dict>
<key>Container</key>
<string>List</string>
<key>Types</key>
<array>
<string>com.apple.applescript.object</string>
</array>
</dict>
<key>ActionBundlePath</key>
<string>/System/Library/Automator/Run AppleScript.action</string>
<key>ActionName</key>
<string>Run AppleScript</string>
<key>ActionParameters</key>
<dict>
<key>source</key>
<string>property enteredWidth : 0
property newPath : ""
on run {input, parameters}
repeat with fileAlias in input
try
display dialog "Enter width for 1x of " &amp; (POSIX path of fileAlias) &amp; ":" default answer ""
set dialogInfo to result
set enteredWidth to (text returned of dialogInfo as integer)
set the newPath to my replace_chars((POSIX path of fileAlias), ".png", "@2x.png")
on error dialogError
return
end try
tell application "Acorn"
set doc to open (POSIX path of fileAlias)
tell doc
trim
duplicate
resize image width enteredWidth
save
close
end tell
tell front document
resize image width (2 * enteredWidth)
save in newPath
close
end tell
end tell
end repeat
return input
end run
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
</string>
</dict>
<key>BundleIdentifier</key>
<string>com.apple.Automator.RunScript</string>
<key>CFBundleVersion</key>
<string>1.0.2</string>
<key>CanShowSelectedItemsWhenRun</key>
<false/>
<key>CanShowWhenRun</key>
<true/>
<key>Category</key>
<array>
<string>AMCategoryUtilities</string>
</array>
<key>Class Name</key>
<string>RunScriptAction</string>
<key>InputUUID</key>
<string>FA71A531-2716-4087-86C3-1A294B5006CE</string>
<key>Keywords</key>
<array>
<string>Run</string>
</array>
<key>OutputUUID</key>
<string>A987B4EB-BA88-4E2C-B272-9A20E19E9B64</string>
<key>UUID</key>
<string>1BB04C1D-F56B-4AFD-8796-DE092148071B</string>
<key>UnlocalizedApplications</key>
<array>
<string>Automator</string>
</array>
<key>arguments</key>
<dict>
<key>0</key>
<dict>
<key>default value</key>
<string>on run {input, parameters}
(* Your script goes here *)
return input
end run</string>
<key>name</key>
<string>source</string>
<key>required</key>
<string>0</string>
<key>type</key>
<string>0</string>
<key>uuid</key>
<string>0</string>
</dict>
</dict>
<key>isViewVisible</key>
<true/>
<key>location</key>
<string>473.000000:748.000000</string>
<key>nibPath</key>
<string>/System/Library/Automator/Run AppleScript.action/Contents/Resources/English.lproj/main.nib</string>
</dict>
<key>isViewVisible</key>
<true/>
</dict>
</array>
<key>connectors</key>
<dict/>
<key>workflowMetaData</key>
<dict>
<key>serviceApplicationBundleID</key>
<string>com.apple.finder</string>
<key>serviceApplicationPath</key>
<string>/System/Library/CoreServices/Finder.app</string>
<key>serviceInputTypeIdentifier</key>
<string>com.apple.Automator.fileSystemObject.image</string>
<key>serviceOutputTypeIdentifier</key>
<string>com.apple.Automator.nothing</string>
<key>serviceProcessesInput</key>
<integer>0</integer>
<key>workflowTypeIdentifier</key>
<string>com.apple.Automator.servicesMenu</string>
</dict>
</dict>
</plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment