Shell script to replicate "Show Package contents" contextual command

Hello,

I’m working on a shortcut (for the Apple Shortcuts app) the would allow me to extract an app bundle’s ICNS file onto the desktop. Shortcuts doesn’t seem to offer a way to dig into the app bundle via native built-in actions, and it seems scripting is the only solution. Unfortunately, I’m don’t know shell scripting.

I basically just need the script to open the package contents so that the next action can drill into Contents/Resources where the ICNS file is usually located.

Thanks.

80sTherapy. Do you require an ICNS file? If a PNG file will do the job, the following might be considered. As written, the specified app has to be running for the shortcut to operate correctly. However, the shortcut could be changed to copy the icon of the frontmost app, or one of several other options.

Copy Icon.shortcut (22.2 KB)

Hi there. Yes, I’m looking to extract the ICNS itself. I found a workaround that allows me to select the app via Quick Actions or Finder by using Sindre Sorhus’s Actions app. It adds a Get File Icon action that extracts the icon in PNG format. I then run the PNG through another shortcut (which you had had made for me if remember correctly!) which converts it to ICNS and saves to desktop.

Thanks for the repeated help!

Cheers.

1 Like

Instead of getting the icon as PNG and then converting it back to an icon, you can also use a Run AppleScript action to return the icon file, where you can save it, for example:

use framework "Foundation"
use scripting additions

on run {input, parameters}
   repeat with anItem in input -- return first result
      tell (current application's NSBundle's bundleWithPath:(POSIX path of anItem))
         if it is not missing value then -- has bundle
            set icon to (its objectForInfoDictionaryKey:"CFBundleIconFile")
            if icon is not missing value then -- has icon
               return (its pathForImageResource:icon) as text as POSIX file
            end if
         end if
      end tell
   end repeat
   return missing value
end run
1 Like

Ah, yes. Scripting is what I was originally thinking. Thank you very much. I will keep both options. Thanks!