Terminal–Finder Interaction

Mac Admins have to work a lot in Terminal. This seems counter-intuitive for an OS that is famed for its user interaction. I can’t talk for all admins, but for me the strength of macOS/OS X was always in the combination of ‘clicky’ UI and command line. When you know what you are doing, you can get the best of both worlds.

I remember an Apple marketing slogan: “The power of Unix, the simplicity of Mac” This is from OS X Lion, so more than five years old by now. The future will show how long Apple still values the ‘powerful’ Unix underpinnings. But for now, they are still available and I am going to use them.

All of that said, the CLI and the UI are not entirely separate areas in macOS, there is a lot of overlap and there are functions in Finder and Terminal that allow for quick interaction between the two.

Finder to Terminal

You can drag any folder from Finder to the Terminal application icon in the dock and Terminal will open a new window and change the working directory to the folder you dragged.

Movie 1: Drag Folder onto Terminal

You can drag the folder icon from the Finder window title bar, as well.

Movie 2: Drag Folder from Title Bar onto Terminal

When you drag any file into an open Terminal window, it will insert the full path to that file or folder with spaces and other special characters properly escaped.

Movie 3: Drag File onto Terminal

You can drag multiple files and Terminal will insert all of their paths, separated by spaces. For example you can type file[space] in Terminal and then drag multiple files into that window and hit return, to get information on the file type.

Movie 4: Drag Multiple Files onto Terminal

If you prefer, you can get the same effect with copy and paste. Just select the files in Finder, choose ‘Copy’ (⌘C), switch to Terminal and ‘Paste’ (⌘V).

Update: I knew I had forgotten one. Thanks to Elliot Jordan who pointed this one out on Twitter:

Dragging a folder into a Terminal window while holding the command (⌘) key will add cd before the path to a folder. When you command-drag a file it will cd to the enclosing folder of that file.

Movie 5: Command Drag a Folder to Terminal

Getting Finder path from Terminal

If you are already in Terminal and want to get the frontmost Finder window, we have to do some homework first. (I got the idea for this script from this post, though I have modified its solution somewhat.) This command

$ osascript -e 'tell app "Finder" to get posix path of ((target of window 1) as alias)'

will give us the correct path, but it has two downsides: a) it is awfully complex to type repeatedly and b) it fails with an error if no Finder window is open.

To avoid typing this long command every time, we have two options. You can either define the command as a function in your .bash_profile (or the respective profile for your preferred shell) or you can save it as a script in your $PATH.

To define it as a function, add this to the .bash_profile:

# prints the path of the front Finder window. Desktop if no window open
function pwdf () {
    osascript <<EOS
        tell application "Finder"
            if (count of Finder windows) is 0 then
                set dir to (desktop as alias)
            else
                set dir to ((target of Finder window 1) as alias)
            end if
            return POSIX path of dir
        end tell
EOS
}

# changes directory to frontmost Finder window
alias cdf='pwdf; cd "$(pwdf)"'

The pwdf function will just print the path to the frontmost Finder window to stdout.

There is also added an alias to quickly change directories to the frontmost Finder window. cdf will also print the path it is changing to, like the cd - command. (Which changes to the previous working directory.) This has to be defined as an alias, since scripts cannot change a shell’s working directory.

If you prefer to define the pwdf command from a script file use the following code:

Save this as a text file without extension in a folder in your $PATH, and set the executable bit with chmod +x /path/to/pwdf. You also need to remove (or comment) the function from your profile, since that would override the script.

Using the script form of osascript allows us to pass arguments into the AppleScript. (You can do that with function as well, but the syntax gets really messy quickly.) This script will list the paths to all open Finder windows with the -a|--all argument. Also when you provide a string as an argument it will search for a Finder window containing that string:

$ pwdf Pref
/Users/armin/Library/Preferences/

The open command

You can also go from Terminal to the Finder. This usually as simple as typing

$ open .

This will open the current working directory ‘.’ in a Finder window.

This is usually where most online ‘hints’ for the open command start and end. However, open has so much more to offer. So much more, in fact, that I will cover the open command in a separate article.

Update for ‘Packaging for Apple Administrators’

I have pushed an update to the Packaging for Apple Administrators book in the iBooks Store. Among a few typos and minor changes, I have added an Appendix section on legacy package formats.

If you already bought the book, you should be able to download the updated content for free. If you buy it now then you will get this update and any future ones!

If you have read and liked the book, please leave a review on the iBooks Store, I would be very grateful!

Get it on iBooks

Mac Manager Meeting – Packaging Presentation – Notes and Links

These are the links for my presentation at the Marriot Library of the University of Utah. You should soon be able to watch an archived version of the presentation here.

Thanks again for letting me speak and for watching and listening!

Slides for the Presentation (as PDF)

Packaging is Dead?

Packaging Tools

AutoPkg

Packaging Book

Mac Managers Meeting Presentation

I will be presenting on packaging Wednesday, Jan 18th (update, I had the wrong date here earlier) at the Mac Managers Meeting of the University of Utah. It will be at 1pm Mountain Time (21:00 Central European). Unfortunately, I will not be present but doing the presentation remotely. There will be a live stream and the talk will be archived to view later.

There will be a brief introduction to some basic pkgbuild and autopkg functions and a more detailed look at the new trust functionality in autopkg 1.0. I hope there will remain time for some Q&A.

Twelvetide, Day 13 (Bonus): Conference

This is the thirteenth part of a twelve-day series on my new year’s resolutions to become a better Mac admin. During these days my book “Packaging for Apple Administrators” is on sale! (Previous Post: “Join the Community”)

Bonus Day! There are many great conferences for Mac admins available right now: (dates are for 2017)

Of course these are great place to learn and watch sessions. Also to meet other admins from many different businesses and schools. Often many of the companies who make software for Mac Administration will have representatives at these conferences showing off their products and available for your questions.

Also these conferences give you a chance to share your knowledge. Most conferences will be very glad for speaker submissions.

Some of these conferences make the session videos and resources available after the conference, these are great resources to watch and learn, even when you do not have the time and money to travel.

(I am sure there are more that I missed, please add them in the comments!)

Also, there are many smaller Mac Admin meetings organised more regularly (or spontaneously) in local groups. The Slack Mac Admin forum can be a great place to find out about these meet-ups!

Twelvetide, Day 12: Join the Community

This is the twelvth part of a twelve-day series on my new year’s resolutions to become a better Mac admin. During these twelve days my book “Packaging for Apple Administrators” is on sale! (Previous Post: “Share and Blog”)

Probably the largest Mac Admin community is the Mac Enterprise mailing list hosted by PSU. This mailing list is mirrored on Google groups if you prefer that interface.

There is also the Freenode IRC # osx-server channel.

However, most of the chat community has moved to the MacAdmins Slack. You can request access to the Slack forum here. You will need the Slack application which is available for Mac and iOS. The Slack forum has several channels for different tools and topics. Slack has a nice help page that’ll introduce to the many options and terms.

My favorite Slack keyboard short cut is “shift-esc” which marks everything as read.

There is also JamfNation. While it obviously focusses on Jamf solutions, it is still a great place to search for generic resources and solutions as well.

Finally, many open source projects have their own mailing lists or forums. For example AutoPkg has a Google group. The Munki project even has two groups: one for general discussion and one for development. Read the project documentation for details. It is often worth searching the group archives for your particular problem before posting and issue against the project.