Archive for the ‘AppleScript’ Category

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: 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.