Shell or AppleScript to emulate Show Package Contents right click menu item

Hello,

this board is always of great help and I’m turning to you once again.

I do some graphic design work and am always digging in Mac app bundles. The only way I know of is right-click > Show Package Contents, but over the course of a day, that becomes tedious.

I’m wondering if a Shell or AppleScript could emulate that command. I would then feed to Keyboard Cowboy (an app similar to BetterTouchTool that allows you to trigger many commands, including scripts) and assign it a keyboard shortcut so that I can just select and type the shortcut to open the bundle.

Is that doable? None of the solutions GPT proposed have worked so far.

Thanks.

tell application "Finder"
	set thePath to POSIX path of (selection as string)
	set theContents to (thePath & "/Contents" as string)
	open (theContents as POSIX file)
end tell
4 Likes

Works liker a charm. Thanks so much!

bogdanw’s suggestion works great and does what 80sTherapy wants. FWIW, the following script works a bit differently in that it does not open a new Finder window.

try
	tell application "Finder"
		activate
		set contentsFolder to ((selection as text) & ":Contents:")
		set target of Finder window 1 to folder contentsFolder
	end tell
on error
	tell me to activate -- just for testing in a script editor
	display dialog "An app must be selected in a Finder window" buttons {"OK"} cancel button 1 default button 1
end try
4 Likes

That’s great! I appreciate you taking the time to post this alternative. Thank you!

Cheers.

One more question. How do I modify your script so that Finder directly opens the Resources folder localed inside the Contents folder. GPT was, again, of not much help…

I don’t know of a way to do what you want. The following uses GUI scripting to expand all folders in the Resources folder, but it’s not just limited to locale folders.

try
	tell application "Finder"
		activate
		set contentsFolder to ((selection as text) & ":Contents:")
		set target of Finder window 1 to folder contentsFolder
		set resourcesFolder to contentsFolder & "Resources:"
		select folder resourcesFolder
	end tell
on error
	tell me to activate -- just for testing in a script editor
	display dialog "An app must be selected in a Finder window" buttons {"OK"} cancel button 1 default button 1
end try

delay 0.5 -- test smaller values
tell application "System Events" to key code 124 using option down -- test without "using option down"

Thank you for your help!

1 Like
use framework "Foundation"
use framework "AppKit"
use scripting additions

tell application "Finder" to set theSel to selection as alias list
if (count of theSel) ≠ 1 then return beep

set thePath to ((POSIX path of item 1 of theSel) & "Contents/Resources/")
set theRez to {"es", "en", "fr"}
repeat with aRez in theRez
	set anURL to (current application's NSURL's fileURLWithPath:(thePath & aRez & ".lproj"))
	if (anURL's checkResourceIsReachableAndReturnError:(missing value)) then
		set theWorkspace to current application's NSWorkspace's sharedWorkspace()
		(theWorkspace's openURL:anURL)
	end if
end repeat

Unfortunately, nothing happens with this script when triggered with a Keyboard shorcut and the app bundle selected in Finder. @peavine 's script above got close. It opens the app bundle’s Contents folder, and even selects the Resources folder located inside of it, but doesn’t actually open it.

80sTherapy. Two possible issues come to mind. The first is that the option+right-arrow keyboard shortcut is not enabled on your computer. This is easily tested outside the script. The second is that the Finder window is losing focus. If that’s the case, the following might be a solution.

try
	tell application "Finder"
		activate
		set contentsFolder to ((selection as text) & ":Contents:")
		set target of Finder window 1 to folder contentsFolder
		set resourcesFolder to contentsFolder & "Resources:"
		select folder resourcesFolder
	end tell
on error
	tell me to activate -- just for testing in a script editor
	display dialog "An app must be selected in a Finder window" buttons {"OK"} cancel button 1 default button 1
end try

delay 0.3 -- test smaller values
tell application "System Events" to tell process "Finder"
	perform action "AXRaise" of the first window
	delay 0.3 -- test smaller values
	key code 124 using option down -- test without "using option down"
end tell

Otherwise, I don’t know.

Ah, GPT came through (for once):

try  
	tell application "Finder"  
		activate  
		set resourcesFolder to ((selection as text) & ":Contents:Resources:")  
		set target of Finder window 1 to folder resourcesFolder  
	end tell  
on error  
	tell me to activate -- just for testing in a script editor  
	display dialog "An app must be selected in a Finder window" buttons {"OK"} cancel button 1 default button 1  
end try

80sTherapy. Glad you found a solution.

BTW, you asked in your earlier post how to open the “Resources folder localed inside the Contents folder.” I assumed that localed was meant to be locales but apparently it should have been located. This was important because Resources folders often contain numerous Localized Project Folders. Anyways, as Chick Hearn used to say, ‘no harm no foul’.

When I saw my quote in @ionah ’s post, I actually started wondering if my typo wasn’t the problem… all working now. Thanks again for the continued help! Cheers.