1

Say I have some files

simple_doc.txt
document.pdf
website.html

The .txt file opens with textEdit by default, the .pdf opens with Adobe Reader and the .html file will open in Google Chrome.

How can I find out this information from the command line? In particular, how can I find the icon of the default application for each of these files from the command line alone.

Thank you!

2

Background

There's a couple of tools you can use to analyze this situation.

  • lsregister
  • defaults read

NOTE: lsregister is located here:

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Versions/Current/Support/lsregister

Navigating lsregister

You can use this command to navigate through the output from lsregister:

$ cd /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Versions/Current/Support/
$ lsregister -dump | less
...
...
    --------------------------------------------------------
    claim   id:            15536
            name:          Plain text document
            rank:          Default
            roles:         Viewer
            flags:         relative-icon-path  doc-type
            icon:          Contents/Resources/document.icns
            bindings:      .txt, .text, 'TEXT', text/plain
    --------------------------------------------------------
...
...

The structure is a little difficult to discern but you can look for strings such as .txt and take note of the lines occurring before it.

To help us navigate this output, we can look in the Finder GUI for a type of file that we're interested in, for example a .txt file. Select it and then open up the info dialog about this particular file ( + I).

For example

                              ss1

With the details such as the "Kind: Plain Text Document" and what application it's associated with to open it, "TextEdit" we can then look through lsregister output and see if we cannot correlate things a bit.

For starters lets look for that "Kind: Plain Text Document".

$ lsregister -dump | grep -A5 -iE 'name:.*Plain text document'
        name:          Plain text document
        rank:          Default
        roles:         Viewer
        flags:         relative-icon-path  doc-type
        icon:          Contents/Resources/document.icns
        bindings:      .txt, .text, 'TEXT', text/plain
--
        name:          Plain text document
        rank:          Alternate
        roles:         Viewer
        flags:         apple-internal  relative-icon-path  doc-type  resolves-icloud-conflicts
        icon:          Contents/Resources/txt.icns
        bindings:      .txt, .text, 'TEXT', text/plain

Above we can see that the icon for the default is this: Contents/Resources/document.icns.

References

0

Here's a programmatic way to retrieve this information:

osascript -e '
    on run {filepath}
        local filepath

        tell application "System Events"
            set A to the default application of the ¬
                item named filepath as alias

            set filename to the value of the property list item ¬
                "CFBundleIconFile" of property list file ¬
                (A & "Contents:Info.plist" as text)
        end tell

        try
            path to resource filename in bundle A
        on error
            try
                path to resource filename & ".icns" in bundle A
            on error
                return 0
            end try
        end try

        POSIX path of result
    end run' /path/to/file

where /path/to/file is the path to any file. The script first determines the default application used to open the file, and then retrieves the application icon from inside the application bundle, returning its path.

You can wrap this inside a bash function like so:

function appicon {
    osascript -e '
       on run {filepath}
          .
          .
       end run' "$1"
}

Then, call it by supplying a path to a file:

appicon ~/Desktop/sunset.png

which, for me, returns:

/Applications/Preview.app/Contents/Resources/Preview.icns

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .