Skip to content

Instantly share code, notes, and snippets.

@benwaldie
Created April 22, 2013 02:02
Show Gist options
  • Select an option

  • Save benwaldie/5431981 to your computer and use it in GitHub Desktop.

Select an option

Save benwaldie/5431981 to your computer and use it in GitHub Desktop.

Revisions

  1. benwaldie created this gist Apr 22, 2013.
    104 changes: 104 additions & 0 deletions 2013-04-21-TUAW_Waldie.applescript
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,104 @@
    -- Ask the user to select an app
    set theApp to choose file of type "app" default location (path to applications folder)

    -- Get the app name
    tell application "System Events"
    set theAppName to name of theApp
    if theAppName ends with ".app" then set theAppName to text 1 thru -5 of theAppName

    -- Determine whether the app is a package, and notify the user if it's not
    set isPackage to (package folder of theApp)

    -- Notify the user if the app is not a package
    if isPackage = false then
    alert("Icons cannot be extracted from " & theAppName & " because it is not a package.") of me
    return
    end if

    -- Locate the app resources folder
    set theAppResourcesFolder to (path of theApp) & "Contents:Resources:" as string

    -- Notify the user if the app resources folder can't be found
    if (folder theAppResourcesFolder exists) = false then
    alert("Icons cannot be extracted from " & theAppName & " because it does not appear to contain a Resources folder.") of me
    return
    end if

    -- Retrieve a list of icon files
    set theIconNames to name of every file of folder theAppResourcesFolder where its name extension = "icns"
    end tell

    -- Notify the user if no icon files were found
    if theIconNames = {} then
    alert("No icon files were found in the Resources folder of " & theAppName & ".") of me
    return
    end if

    -- Ask the user to choose the icons to copy
    if length of theIconNames = 1 then
    set theIconsToExtract to theIconNames
    else
    set theIconsToExtract to (choose from list theIconNames with prompt "Which icons would you like to extract from " & theAppName & "?" default items theIconNames with multiple selections allowed)
    end if
    if theIconsToExtract = false then return

    -- Ask whether the user would like icon files or PNG files
    set theIconType to button returned of (display alert "Extract Icon Files" message "Would you like to extract icons in INCS or PNG format?" buttons {"Cancel", "PNG", "ICNS"} cancel button "Cancel")

    -- If a PNG file is preferred, ask the user what size they would like
    if theIconType = "PNG" then
    set theSizes to {"16", "32", "128", "256", "Actual Size"}
    set theSizeChoice to (choose from list theSizes with prompt "What size would you like for the PNG icons?" default items {"Actual Size"})
    if theSizeChoice = false then return
    set theSizeChoice to item 1 of theSizeChoice
    end if

    -- Create an output folder for the icons
    set theOutputFolder to makeFolder("Extracted Icons", path to desktop folder as string)
    set theOutputFolder to makeFolder(theAppName & " Icons", theOutputFolder)

    -- Extract the icons to the output folder
    repeat with a from 1 to length of theIconsToExtract
    set theCurrentIconFileName to item a of theIconsToExtract
    set theCurrentIconFile to theAppResourcesFolder & theCurrentIconFileName as string

    -- If an icon file is preferred, copy it to the output folder
    if theIconType = "ICNS" then
    tell application "Finder"
    duplicate theCurrentIconFile to theOutputFolder
    end tell

    -- If a PNG is preferred, convert the icon to PNG, scale it if necessary, and save it to the output folder
    else
    set theCurrentIconFileNamePrefix to theCurrentIconFileName
    if theCurrentIconFileNamePrefix ends with ".icns" then set theCurrentIconFileNamePrefix to text 1 thru -6 of theCurrentIconFileNamePrefix
    set theSavePath to (theOutputFolder as string) & theCurrentIconFileNamePrefix & ".png"
    tell application "Image Events"
    set theImage to open theCurrentIconFile
    tell theImage
    if theSizeChoice is not equal to "Actual Size" then scale to size theSizeChoice as string
    save as PNG in theSavePath
    end tell
    end tell
    end if
    end repeat

    -- Let the user know the icons have been extracted and display the output folder
    display alert "The specified icons have been extracted."
    tell application "Finder" to open theOutputFolder

    -- This handler displays an alert
    on alert(theMessage)
    display alert "Unable to Extract Icons" message theMessage as warning
    end alert

    -- This handler makes a folder in an output folder
    on makeFolder(theFolderName, theOutputFolder)
    tell application "System Events"
    if (folder theFolderName of folder theOutputFolder exists) then
    return path of (folder theFolderName of folder theOutputFolder)
    else
    return path of (make new folder at folder theOutputFolder with properties {name:theFolderName})
    end if
    end tell
    end makeFolder