AppleScript to show package contents of hidden original of alias

aliasapplescript

I'm attempting to build an Automator workflow (which will be saved as a Service, so that I can both access it from contextual menus and invoke it with a keyboard shortcut), that will function much like the Finder's built-in "Show Package Contents" command, but which will additionally accept a package's alias as input. The service will satisfy the following:

It will only be available when the Finder's selection consists of a package or a package's alias.

Rather than show the contents of the package directory itself like the built-in command does, the Service will show the contents of the "Contents" directory (if it exists), located immediately within the package directory.

If the Finder's selection is a package or alias on the desktop, the Contents directory should be opened in a new Finder window. Otherwise, the Contents directory should be opened in the Finder window containing the selection.

My approach has been to have the script first determine whether the file is an Alias, and if it is, to tell the script to show the contents of the original. If the file is not an alias, I'd like to tell the script to simply show the contents of the input.

Excluding the possibility of an alias as input and assuming the input-package is selected in a Finder window, I can successfully write a script that shows the contents of the package's contents folder in the same window:

on run {input, parameters}
    set my_output to {}
    repeat with oneItem in input
        tell application "Finder" to set target of window 1 to ((oneItem as text) & "Contents")
end repeat
return input
end run

When I try to set up an "if…then…else" scheme, I fail.

I'm using the latest versions of all relevant software.

Best Answer

folder "Contents" of also works with aliases:

on run {input, parameters}
    tell application "Finder"
        repeat with f in input
            open folder "Contents" of f
        end repeat
    end tell
end run

You can also assign a shortcut for showing package contents from System Preferences:

Command-R shows the original file of an alias.