By Jason Snell
May 24, 2019 5:32 PM PT
Scripting a save location within Default Folder X
I’ve been using St. Clair Software’s Default Folder since the days of System 7. It’s a utility that lets you set a different default location for the Open/Save dialogs in every app you use, and provides some other clever features like clicking on an open Finder window to change the Open/Save dialog to that window’s location.
For the first time in years, a few weeks ago I had a feature request for Jon Gotow, the developer of Default Folder. I realized that in at least one app I use, there’s a very common location I want to save all my files—but the location is not persistent. When I’m recording ads and other audio for podcasts, I invariably end up saving it all in the Audio Files folder in the project folder for my current Logic project, which is invariably sitting on the Desktop.
So I asked Jon, is there any way to programmatically define where Default Folder opens? His response was to send me a development build of Default Folder that would allow the default location to be overridden with an AppleScript, a “hidden” feature now available in version 5.3.7 of Default Folder X. Now that is customer service.
In any event, I now needed to write the script, which means I needed to figure out the specific rules that would define the right destination folder. In the end I realized that what I needed to do was find the most recently modified folder on my Desktop that contained a Logic X project file. AppleScript, while perhaps not the best tool for this job, is the tool that I know how to use.
on getDefaultFolder(appName) tell application "Finder" set folder_list to folders of (path to desktop folder as alias) end tell set theNewestDate to date "Tuesday, October 6, 1970 at 7:00:00 AM"
This first part gets a list of folders on the Desktop, and then sets a variable to a very old date for reasons that will make sense in a little while.
repeat with theFolder in folder_list tell application "Finder" set folder_contents to entire contents of theFolder end tell repeat with the_item in folder_contents if kind of the_item is "Logic X Project" then set theDate to (get modification date of theFolder) if theDate is greater than theNewestDate then set theNewestDate to theDate end if end if end repeat
The next part loops through those folders, getting their contents. The script then loops through those contents to see if there’s a Logic project file inside. If so, the script compares that folder’s modification time to the contents of the variable
theNewestDate, and if it’s more recent, that variable is updated to the modification time of the newer folder.
(In writing this script I discovered that AppleScript does let you compare dates, using the fantastic construction “if [a date] is greater than [another date].” Greater in this case means newer.)
if theNewestDate is date "Tuesday, October 6, 1970 at 7:00:00 AM" then set theResult to (path to desktop folder as alias) else tell application "Finder" set theDestinationFolder to (every item of (path to desktop folder as alias) whose modification date is theNewestDate) end tell set theResult to POSIX path of (item 1 of theDestinationFolder as alias) & "Audio Files" end if end repeat return theResult end getDefaultFolder
The final step (after making sure that there is a folder on the Desktop containing a Logic project, because if
theNewestDate is still 1970, there isn’t one) is to find that newest folder, append the name of the Audio Files subfolder to it, and pass that result back to Default Folder.
The result is somewhat magical: Now when I record audio in Sound Studio and press save, the save dialog box bounces to the Audio Files folder within the newest Logic project folder on the Desktop. Nine times out of ten, it’s exactly where I want to be.
[If you appreciate articles like this one, help us continue doing Six Colors (and get some fun benefits) by becoming a Six Colors subscriber.]