Archive for the ‘Automation’ Category

Customizing the suggested filename in Save dialogs

Thursday, July 25th, 2024

I got a request from a Default Folder X user yesterday, asking if I could add a feature to limit the length of the default filename supplied in Save As dialogs. Apparently, the cloud services and backup software that he and his colleagues use can’t handle filenames longer than 45 characters, so he wants to make sure they don’t save files with names longer than that. That’s a pretty unfortunate limitation of the software they’re using (I mean really, in 2024?), but I’m all for avoiding stupid problems.

I’ve addressed a similar request before, automatically prepending dates to the names of files as you save them. This post is an update, because Default Folder X now has a better way to handle this than the method described in that earlier post.

Background

Default Folder X has a flexible, AppleScript-based method for handling customized default folders and filenames in Save dialogs. Yeah, I know AppleScript is old and crufty, but it’s available on every Mac, can interoperate with other apps, and allows you to cobble together just about anything, so it’s a good solution here. Anyway, when a file dialog appears on the screen, Default Folder X looks for and runs special AppleScripts if they’re present, allowing you to use any logic you want to determine the starting location (the “default folder”) for a file dialog, and the name of the file suggested in Save As dialogs.

The AppleScripts should be located in this folder:

     ~/Library/Application Support/com.stclairsoft.DefaultFolderX5/Scripts/

where the ‘~’ character denotes your home folder. Default Folder X looks for scripts with predetermined names, depending on their purpose:

  • getDefaultFilename.scpt – modifies the suggested filename in Save As dialogs
  • getDefaultFolder.scpt – specifies the default folder for an Open or Save dialog

Note that the scripts must be saved with those names in order for them to work. If you want a script to only run in the Open and Save dialogs of a single application, put it in a subfolder with the same name as the app. For example, to have scripts that only run in Safari, save them in:

     ~/Library/Application Support/com.stclairsoft.DefaultFolderX5/Scripts/Safari/

The implementation details (the name of the AppleScript handler and the arguments that Default Folder X passes to it) are included at the beginning of each of these sample scripts:

Truncating Long Filenames

OK, back to the task at hand. In this case, we want to provide a “getDefaultFilename” handler that truncates the default filename in Save dialogs to no more than “maxLength” characters.

Here’s the AppleScript:

on getDefaultFilename(appName, suggestedName, fileExtension)

  -- Specify our maximum filename length
  set maxLength to 45
  
  -- Make sure the filename extension isn't counted
  set addExtension to false
  if fileExtension is not "" and suggestedName ends with fileExtension then
    set suggestedName to text 1 thru -((count fileExtension) + 2) of suggestedName
    set addExtension to true
  end if
  
  -- Truncate the filename
  if length of suggestedName is greater than maxLength then
    set newName to text 1 thru maxLength of suggestedName
  else
    set newName to suggestedName
  end if
  
  -- Add the extension back on if appropriate
  if addExtension is true then
    set newName to newName & "." & fileExtension
  end if
  
  -- And give the name to Default Folder X
  return newName

end getDefaultFilename

Enter the code above in Script Editor and save it as an AppleScript script or download GetDefaultFilename.zip. Save the “GetDefaultFilename.scpt” file in this location:

     ~/Library/Application Support/com.stclairsoft.DefaultFolderX5/Scripts/

Each time a Save As dialog appears, it will magically truncate any filenames longer than 45 characters. Note that you can still edit the name afterwards in the Save dialog if you don’t like the one created by the script.

Credit Where Credit is Due

This scheme originated with an idea from Jason Snell (of Six Colors and The Incomparable fame) when he needed to customize the way Default Folder X worked in his podcasting workflow. I later expanded it as requests came in for customized filenames as well, and Bo Engelbrecht’s email reminded me that I’d never blogged about those updates. Thanks Jason and Bo!

Default Folder X: Opening folders in Path Finder vs. Finder

Wednesday, June 2nd, 2021

With the recent focus on Default Folder X’s integration with Path Finder, I’ve been fielding a number of questions about how to make Default Folder X open folders in Path Finder instead of the Finder.

Using Path Finder instead of the Finder for all apps

The first, simplest answer is that Default Folder X uses your “default file browser” when opening folders. If you set your default file browser to be Path Finder, selecting a folder from Default Folder X’s menus will open it in Path Finder. This will also make all other apps on your Mac use Path Finder for their “Reveal in Finder” commands.

“But how the heck do I set Path Finder as my default file browser?” you say. Well, I’m glad you asked! It’s easy – there’s a setting in your Path Finder preferences:

Turn on this checkbox and you’re done.

Using Path Finder instead of the Finder – but just for Default Folder X

If you’d rather make this apply only to Default Folder X, you can set Default Folder X’s “fileViewer” preference in Terminal with this command:

defaults write com.stclairsoft.DefaultFolderX5 fileViewer com.cocoatech.PathFinder

Note that if you’re using the Setapp version of Path Finder, you should replace ‘com.cocoatech.PathFinder’ with ‘com.cocoatech.PathFinder-setapp’. To tell Default Folder X to go back to using the Finder instead of Path Finder, just replace ‘com.cocoatech.PathFinder’ with ‘com.apple.finder’.

Toggling between Path Finder and Finder on the fly

And finally, if you want to get really fancy and sometimes have Default Folder X open folders in the Finder and sometimes in Path Finder, you can set up an AppleScript to toggle back and forth. Attaching a script like this to a keyboard shortcut using Peter Lewis’ amazing Keyboard Maestro app makes it super-easy:

-- set the 'currentViewer' variable to the current fileViewer setting
set currentViewer to do shell script "defaults read com.stclairsoft.DefaultFolderX5 fileViewer"
-- now switch to whichever fileViewer is currently not in use
if currentViewer is "com.apple.finder" then
do shell script "defaults write com.stclairsoft.DefaultFolderX5 fileViewer com.cocoatech.PathFinder"
else
do shell script "defaults write com.stclairsoft.DefaultFolderX5 fileViewer com.apple.finder"
end if

Automatically prepending dates to the names of saved files

Wednesday, September 9th, 2020

So a Default Folder X user just emailed and asked this:

I have been a Mac user for 30 years and would love to find a tool that allows me to click a button (or make this the default filename) while in the “Save…” dialog box that will prepend a formatted date to the beginning of the filename. like so:

2020-09-08 Filename.ext

Now, you can set up an AppleScript to do this using Default Folder X’s GetSaveName and SetSaveName verbs. However, that would require that you run the AppleScript whenever you want the date prepended, which is a bit of a pain if you want all of your filenames formatted this way. But I realized as I was replying that you can actually automate this by using (or rather, slightly abusing) an existing feature in Default Folder X.

Default Folder X has the ability to run an AppleScript to determine the location of an application’s default folder. The script will be run whenever a new file dialog is displayed by an application, which is the perfect time to do our little filename modification. So I wrote an AppleScript that looks like this:

on getDefaultFolder(appName, dialogType, firstTime)

  -- only do this for save dialogs
  if dialogType is "save" then

    -- get the current date
    set dateObj to (current date)

    -- then format it as YYYY-MM-DD
    set theMonth to text -1 thru -2 of ("0" & (month of dateObj as number))
    set theDay to text -1 thru -2 of ("0" & day of dateObj)
    set theYear to year of dateObj
    set dateStamp to "" & theYear & "-" & theMonth & "-" & theDay

    -- then prepend that to the name in the save dialog
    tell application "Default Folder X"
      set theName to GetSaveName
      set theName to dateStamp & " " & theName
      SetSaveName theName
    end tell
  end if

  -- finally, don't give Default Folder X a default
  -- folder, so it just continues on normally 
  return ""
end getDefaultFolder

If you save this script in a file named “GetDefaultFolder.scpt” in this location:

~/Library/Application Support/com.stclairsoft.DefaultFolderX5/Scripts/

It will magically prepend the date in the format ‘2020-09-15’ to the beginning of all of your filenames in Save As dialogs. Note that you can still edit the name afterwards if the default filename (like “Untitled 4”) needs to be modified.

App Tamer 2.5.1 adds more AppleScript support, fixes issues

Monday, May 18th, 2020

Version 2.5.1 of App Tamer is available now. Among other things, it includes fixes for a couple of complaints with the “using too much CPU” notifications that App Tamer puts up when a process is – you guessed it – using too much CPU. It will no longer notify you if you’ve already throttled an app, even if the app is still over the warning threshold. It also provides a method of making the “Let it continue” button suppress the high-CPU notifications for longer. The default is now 10 minutes (instead of 5) before you see another warning, and you can change that by using this command in Terminal:

defaults write com.stclairsoft.AppTamer notificationMuteTime XXX

where XXX is is the number of seconds to silence notifications.

And for those folks that want to automate control of their apps, a new “manage” verb in App Tamer’s AppleScript dictionary lets you create scripts so you can change settings on a schedule, change an app’s settings with a keyboard shortcut, or something AppleScript-y like that. Here’s an example:

tell application "App Tamer"
manage "Safari" slow yes slowCPU 2 hide yes hideDelay 10
end tell

That will slow Safari to 2% CPU usage when it’s in the background and will hide it after it’s been idle for 10 minutes. To see all of the options, open App Tamer’s dictionary in Script Editor.

This scripting ability is being used by some users to change settings for backups so they run with different CPU limits at night vs. during the day, and throttling background apps more aggressively during video calls. As they say, the possibilities are endless!

App Tamer 2.5.1 also includes a number of fixes for infrequently encountered bugs, such as incorrect behavior when the stats update frequency is set to “never”, and processes not appearing when they’re run from the Terminal using ‘sudo’ or ‘su’.

For a full list of changes and download links, visit the App Tamer release page.

Default Folder X 5.4.3: Better performance, better previews, and a bunch of fixes.

Friday, March 6th, 2020

Version 5.4.3 of Default Folder X, our app for managing files and folders in Open and Save dialogs and the Finder, is now available. This release speeds things up when opening items in the Finder, ForkLift and Path Finder, as well as when saving files to slow servers over a network. It also brings little improvements in several areas:

  • The on-the-fly previews (the ones you see when traversing Default Folder X’s menus, in its Finder drawer, and in Open dialogs) have been improved to look better and display more smoothly.
  • It’s now easy to make Default Folder X forget all of your recently used files, folders and Finder windows all at once. Just hold down the Option key when choosing “Forget Recent <whatever>” at the bottom of a menu, and the menu command will change to “Forget All Recent Data”.
  • For any AppleScripters out there, there are new commands for managing files and folders in DFX’s Finder drawer, and an option to pop up its menu at a specific screen location. Note that if you’re using a macro utility that can run AppleScripts (like Peter Lewis’ excellent Keyboard Maestro), this can make it really handy to get to Default Folder X’s menus without going up to the menu bar.

There are also more than a dozen bug fixes, covering everything from occasional reliability issues to more esoteric problems with Pro Tools, Rogue Amoeba’s Fission app, and the built in screen capture utility in Mojave and Catalina. Oh, and Default Folder X’s Finder-click feature will now recognize all the tabs in Finder windows that aren’t in the current Space (if you’re using Mission Control to manage multiple workspaces). That was a really weird one.

Itemized release notes with all the details are available on the Default Folder X Release page, as usual, along with download links.

Default Folder 5.4 for Catalina, with Path Finder SetApp support, improvements for scripters and more

Monday, October 7th, 2019

I’m happy to announce that the final version 5.4 of Default Folder X is now available. Thank you to everyone who beta tested the pre-release versions and reported issues!

The marquee feature of this release is, of course, support for macOS 10.15 Catalina, which Apple should drop any day now. In addition, there are a couple of new AppleScript commands in Default Folder X’s scripting dictionary to help scripters automate the handling of file dialogs (and don’t forget the scriptable default folders too). This version also adds support for the version of Path Finder distributed via SetApp.

Finally, there are a handful of bug fixes, including corrections for issues with Finder windows, adding new Favorites, and Accessibility quirks. These fixes apply to both Catalina and earlier macOS versions – if you’re running an older version of macOS, you can still update to Default Folder X 5.4. It supports anything from macOS 10.10 to 10.15.

The update is free if you’ve already got a license for Default Folder X 5 – just choose “Check for Updates” from Default Folder X’s menu, or download a copy here. A list of changes and download links, including localized versions, are available on the Default Folder X release page.

Default Folder X: Using AppleScript to specify default folders on the fly

Tuesday, May 28th, 2019

Version 5.3.7 of Default Folder X introduced a new capability: it can now ask what the default folder for an application should be on the fly using AppleScript. That may sound like a mouthful of jargon, so let me explain, because it can be applied in a lot of situations.

Jason Snell (of Six Colors and The Incomparable fame) has been writing about Macs forever, and is now a prolific podcaster. He emailed to ask if it would be possible to make Default Folder X more flexible. At that time, you could set a default folder for an application so that when you chose Save As, it always offered to save a file in that particular folder. His problem was that you have to set a single folder as the app’s default folder – just one.

Jason creates podcasts – lots of them. His reasoning was that if he could magically tell Default Folder X what podcast he was working on, it would always offer to save the component audio files into the folder for that podcast. Essentially: Wouldn’t it be great if you could edit an audio clip, hit Save, and have it automatically go into the folder for the current podcast folder? No re-configuring a default folder for each new project – it’d just work.

So he hit on this idea (which I think is just brilliant). He uses Apple’s Logic X application to create his podcasts. So for each podcast episode, there’s one master Logic X project file. To find the correct default folder for audio clips, all he has to do is look at all the project files and see which one has been saved most recently. The “Audio Files” folder sitting next to that project is where everything should go for the current project. He wrote an AppleScript to do this, which he shared on the Six Colors blog.

This can obviously translate to all sorts of different workflows. If you have one primary file for each project, it’s easy to tell which one you’re currently working on – it’s the one that’s been saved most recently.

How to set up an AppleScript to specify a default folder

So how do you wire up Default Folder X to do this? It’s pretty simple. Put an AppleScript script in:

~/Library/Application Support/com.stclairsoft.DefaultFolderX5/Scripts/

or, if you want it to handle only a single application, put it in a sub-folder of the Scripts folder named for the application you want it to serve. To have it queried only for Preview, for example, put an AppleScript in:

~/Library/Application Support/com.stclairsoft.DefaultFolderX5/Scripts/Preview/

Download this example script file to see what you need to do in your AppleScript script:

http://www.stclairsoft.com/download/GetDefaultFolder.zip

The trick is that you need to implement this handler:

on getDefaultFolder(appName, dialogType, firstTime) 

which returns the location of the default folder. Open up the sample script file in Script Editor (or your AppleScript editor of choice) and have a look. I’ve tried to explain things clearly in the comment at the top, and the script shows a number of different ways of returning folders to Default Folder X. And of course, you can also use Jason’s complete script as a starting point.

I think Jason’s idea is great – it streamlines work on multiple projects, but most importantly, it reduces the chance for error as you’re trying to meet that pressing deadline. I’d love to hear how others use this feature, so please drop me a line if it works for you too!

Default Folder X 5.3.7 updates Finder window tracking, shows additional metadata and more

Thursday, May 16th, 2019

Default Folder X 5.3.7 is now available, and it displays a couple of additional pieces of metadata in the Info panel below Open dialogs, most notably the “last opened” date. It also addresses a number of issues, including problems with LaunchBar, sub-par behavior when file dialogs are very large or lie partially off-screen, keyboard shortcuts not working after using a menu bar app, and drag-and-drop problems with the Finder drawer. A full list of changes is available on the Default Folder X Release page or in the Version History.

This version also works around bugs in Mojave that have been affecting Default Folder X’s ability to list open Finder windows when those windows contain multiple tabs. It will now list those windows reliably, but may still get confused and show some tabs as being in their own, separate windows – but hey, at least they’re all there, right? Unfortunately, a complete solution requires that Apple fix the bugs that I’ve submitted.

And one very important note about Finder windows: The behavior of Default Folder X’s Finder-click feature has changed a bit. Most people won’t be affected by this, but if you have been relying on the fact that Finder-click showed windows that weren’t actually visible (because they’re in another Space or because the Finder’s hidden), you’ll find that they’re no longer appearing. They’re still in the Finder Windows menu in Default Folder X’s toolbar, or you can revert to the old behavior by following these instructions.

Finally, on the truly geeky side, you can now create an AppleScript to supply Default Folder X with a default folder for an application on the fly. When a file dialog comes up, DFX will run your AppleScript, and if it returns a folder, that’ll be used as the default folder for that file dialog. It works seamlessly and can really simplify things if you work in a project-based manner with a consistent way of determining where your project folder is. Look for a blog post about this shortly.