AppleScript to delete APFS Local Snapshots

Here’s an AppleScript I’ve been using to view listings for local snapshots then delete unwanted ones for my APFS boot volume in High Sierra. You’ll be prompted for your admin password for deleting snapshots.

set IDList to paragraphs of (do shell script "tmutil listlocalsnapshots /")
set BL_1 to {}
repeat with i in IDList
set end of BL_1 to ((word 2 of i & "-" & word 3 of i & "-" & word 4 of i & "-" & word 5 of i) as text)
end repeat
if (count of BL_1) is not 0 then
choose from list BL_1 with prompt "Select snapshots to delete. Select as many as desired. Enter administrator password when prompted." with multiple selections allowed
set theSelection to result
if (count of theSelection) is not 0 then
repeat with theItem in theSelection
do shell script ("tmutil deletelocalsnapshots" & space & theItem) with administrator privileges
end repeat
end if
display dialog "There appear to be no snapshots to delete." buttons {"OK"} default button 1
end if



AppleScript: Clipboard to text file on Desktop

Clipping files created by dragging a text selection to the desktop are very useful, but sometimes it’s more beneficial to create a text file. Here’s an AppleScript that does that. The script is most conveniently used by activating it with a keyboard shortcut or other shortcut rather than having to find it in your Script menu. One way to make it quickly accessible is to place it in the toolbar of Finder windows. The way I run it is to use ⇧⌃⌥L (Shift-Ctrl-Opt L) in Keyboard Maestro. You could also put it into the Script Menu and launch it using Daniel Jalkut’s FastScripts, or you could even create a System Service from the AppleScript using Automator and configure the keyboard trigger in the Shortcuts tab of the Keyboard sysprefpane (System Preferences> Keyboard). Whatever way you choose to run it, all you need to do is make sure the text you want turned into a file is copied to your clipboard first. The Finder window toolbar and Automator System Service methods are built into the Mac system and are completely free. Keyboard Maestro and FastScripts are commercial automation solutions, but both are well worth the investment.


property temppath : "/private/tmp/"

property startnum : 0

property clipname : "clip"

property add_on : ".txt"


set clipname to (text returned of (display dialog "Enter a description of the clipboard text that will be used to name the file:" default answer clipname))

set rootpath to ((path to desktop) as Unicode text)

set thepathname to rootpath & clipname

set pos_filepath to POSIX path of rootpath as Unicode text

set dest_file to (quoted form of (temppath & clipname & add_on))

do shell script "pbpaste >" & space & dest_file

tell application "Finder"

set basename to clipname



set name_target to alias (rootpath & clipname & add_on) --file name is already taken at destination if no error occurs.

set startnum to startnum + 1

set clipname to text returned of (display dialog"A file with that name already exists. You may enter a new name or cancel."default answer (basename & startnum))

on error _error

set startnum to 0

if _error contains clipname then --if target is clear and source file can be copied from temp directory

tell me to do shell script"ditto" & space & dest_file & space & (quoted form of (pos_filepath & clipname & add_on))

exit repeat

else -- if action is cancelled by user

exit repeat

end if

end try

end repeat

end tell


If you ever get tired of holding down a key or a combination of keys to make your Mac observe any of several modes or behaviors at startup, you might be interested in preparing a few keysinkers. For example, you could use a couple of keysinkers to boot into Recovery Mode while you go fetch a cup of coffee. You can readily construct keysinkers using cheap fishing tackle (line sinkers) and Sugru or Oogoo.

Just adapt the bottom of the sinker to give it a flat surface and make it lean enough to accommodate the angle of your particular keyboard’s surface. I’ve been able to compromise the angle so that my keysinkers can remain stable for different degrees of keyboard tilt.

Illustration of keysinkers on Mac keyboard keys



Droplet for How Long an App has been Running

The Robservatory blog featured a method for finding out how long an app has been running. Here’s the source for an AppleScript application that lets you drag and drop your running app’s icon to see that information. Put this script into an AppleScript Editor (currently known as “Script Editor“) document, then save it as an application. Don’t copy the line numbers. Be sure your app is running, then drag that app’s icon onto the new app you created.

Warning: If the running process of the app doesn’t have the same name as the app’s icon (excluding the “.app” extension, of course), the droplet won’t work.

Previously, I discovered a problem with some apps with longer names, and provided a revised version. Now I’ve added the ability to select an app icon in the Finder and launch this app from the Dock or the Script menu for those of you who might prefer not to drag your app icons.

on open the_item
	my build_answer(the_item)
end open

on run
	tell application "Finder"
		set the_item to get selection as string
	end tell
	set the_item to get the_item as alias
	my build_answer(the_item)
end run

on build_answer(the_item)
	--	tell me to activate
	set item_info to get info for the_item
	set _choice to characters 1 thru -5 of (displayed name of item_info) as string
		set _pids to paragraphs of (do shell script "/bin/ps -acxwwl")
		set ls to {}
		set ps to _choice
		repeat with _line in _pids
			if _line is "" or _line contains "TIME COMMAND" then
				set _pid to 2nd word of _line
				set s to last text item of _line
				set n to seventh word of _line
				if s is "t" then set s to "init"
				tid("     ")
				set _process to s
				set _ful to _pid & tab & n & tab & _process
				if _ful ends with ps then
					set end of ls to (_pid & tab & n & tab & _process) as string
				end if
			end if
		end repeat
		set _pit to item 1 of ls
		set _pid to word 1 of _pit
		set _uptime to (do shell script "ps -o etime= -p" & space & quoted form of _pid)
		set time_val to count words of _uptime
		set _secs to word time_val of _uptime
		set _mins to word (time_val - 1) of _uptime
		if (time_val - 2) > 0 then
			set _hours_string to ((time_val - 2) as string) & space & "hour(s)," & space
			set _hours_string to ""
		end if
		if (time_val - 3) > 0 then
			set _days_string to ((time_val - 3) as string) & space & "day(s)," & space
			set _days_string to ""
		end if
		display dialog "The" & space & _choice & space & "app has been running for" & space & _days_string & _hours_string & _mins & space & "minute(s)" & space & "and" & space & _secs & space & "second(s)." buttons {"OK"} default button 1
	on error
		display dialog "Something went wrong. Are you sure the app of the icon you dropped is really running?." buttons {"OK"} default button 1
	end try
end build_answer

on tid(s)
	set my text item delimiters to s
end tid

You can still remove annoying agreement prompts from Mac disk images (.dmg files)

I got tired of accepting agreement notices when opening distributed disk image (.dmg) files, so I created this script. It should work until certain people decide it shouldn’t. Create a MacOS Service using Automator with one Run AppleScript action, and make the service accept documents in Finder. Paste the following AppleScript into the Run AppleScript action and save. Here’s the shortest complete documentation I could find (written by Ben Waldie, an extremely proficient Mac automation enthusiast) that explains how to use Automator to quickly deploy AppleScripts as MacOS Services.

After you’ve added the MacOS Service, when you encounter a disk image that you’ve downloaded that wants you to agree to something before it opens, just click “disagree”, then launch the service from the contextual menu (right-click the disk image). After a few seconds (depending on the size of the image), you can double-click it and it will open with no further nagging.

property temppath : "/private/tmp/"

on run {input}

 (* Your script goes here *)
 repeat with the_item in input
 end repeat
end run

on build_archive(the_item)
 --repeat with the_item in the_items
 set the_item to the_item as alias
 tell application "Finder"
 set sost to ((container of file (the_item as string)) as alias) as string
 end tell
 set sost to POSIX path of sost
 on error therrr
 log therrr
 display dialog therrr
 set sost to "/Volumes/"
 end try
 set this_filepath to (the_item as string)

 if last character of this_filepath is ":" then
 tell me to set it_is_a_folder to true
 set it_is_a_folder to false
 end if
 if not it_is_a_folder then
 set thesourcename to (name of (info for the_item))
 set the_source_file to POSIX path of this_filepath
 set pos_filepath to sost
 --set thesourcename to replace_chars(thesourcename, " ", "_")
 set dest_file to (quoted form of (temppath & thesourcename))
 set tempname to ((characters 1 thru -5 of thesourcename) & "temp")
 set my_command to "hdiutil convert -format UDRW" & space & (quoted form of the_source_file) & space & "-o" & space & (quoted form of (temppath & tempname))
 do shell script my_command
 set my_2nd_command to "hdiutil convert" & space & (quoted form of (temppath & tempname & ".dmg")) & space & "-format UDZO -imagekey zlib-level=9 -o" & space & (quoted form of (temppath & thesourcename))
 do shell script my_2nd_command
 do shell script "rm" & space & (quoted form of (temppath & tempname & ".dmg"))
 on error onerr
 display dialog onerr
 end try

 if pos_filepath is "/Volumes/" then
 set pos_filepath to (POSIX path of (path to desktop folder))
 end if
 set targdetect to (pos_filepath & thesourcename)
 set name_target to alias (POSIX file targdetect)
 --use this instead of above to prevent overwriting your original disk image: set finishname to "_recomp"
 set finishname to ""
 set thesourcename to (((characters 1 thru -5 of thesourcename) & finishname) as Unicode text)
 tell me to do shell script "ditto -rsrcFork" & space & dest_file & space & "\"" & (pos_filepath & thesourcename) & ".dmg" & "\""
 end if
 do shell script "rm" & space & dest_file
 end try
 --end repeat
end build_archive

Fix Home Folder Permissions

Here’s an AppleScript to help you regain control of your user home folder if permissions have been corrupted by performing certain actions such as installing software that doesn’t respect proper home folder permissions or by drag copying items when the current context for maintaining proper permissions is incorrect. Symptoms of this problem can range from not being able to see on the desktop items that are known to exist in the Desktop folder or not being able to move icons for desktop items to being unable to copy or move items inside the home folder.

The current user account must be an admin account when the script is used. If the account needing repair is not currently an admin account, you can use the Users & Groups prefpane to make it an admin account temporarily to run the script (providing, of course, that you have access to an admin account).

set username to do shell script "id -un"

do shell script "chown -R" & space & username & space & "~;chmod -RN ~;chmod -R o-w ~;chmod +a \"everyone deny delete\" ~/ ~/Desktop ~/Documents ~/Downloads ~/Library ~/Movies ~/Music ~/Pictures ~/Public ~/Sites;chmod +a \"" & username & space & "allow list,add_file,search,delete,add_subdirectory," &  "delete_child,readattr,writeattr,readextattr," &  "writeextattr,readsecurity,writesecurity,chown,file_inherit," & "directory_inherit\" ~/Public/Drop\\ Box" with administrator privileges