Writing the settings that need to be saved can be done withto a .plist
.plist
file, which stores key/value pairskey/value pairs of data along with the data types. This allows any value to be saved and retrieved by its name. They're also relatively simply to read and edit programmatically using either AppleScript or the command line utility plutil
.
Cleverlywhere the variable OpenApps
is taken from the AppleScript snippet above. Cleverly, the make new property list
command will overwriteoverwrite any existing key/value item that already has the name "OpenApps"
, so it won't be duplicated.
Bash:
lsof | egrep -i -e 'windows\.plist'
Doesn't include menu bar apps.
lsof
= list of open files, filtered for windows.plist
files that each open application (with a window) updates regularly. When the application closes, so does the .plist
file.
Slow, and not really designed for this purpose, but useful to be aware.
This filter pattern retrieves an exhaustive list of all running user apps, including menu apps:
lsof | egrep -i -e '/Applications/.+\.app/Contents/MacOS/[^/]+$'
ps
is fast and accurate:
IFS=$'\r\n'; basename $( ps -U 501 -o 'comm' | egrep -i '^/Applications/.*\\.app/Contents/MacOS/[^/]+$' ) | sort -u
lsappinfo visibleProcessList | egrep -io -e '"[^"]+"' | tr -d \" | tr '_' ' '
IFS=$'\r\n'; basename -s '.app' $(lsappinfo list | egrep -io -e '(\w|\s)+\.app"$' | tr -d \")
lsappinfo find | egrep -io -e '"[^"]+"' | tr -d \" | tr '_' ' '
That last one is every single running application process (87 of them on my system right now)
.savedState
files
Some degree of usefulness in obtaining open documents. The relevant file is windows.plist
contained within theI Noticed that some .savedState
folder. Thefiles are stored in plist~/Library/Saved Application State
as aliases (symbolic links) instead of the actual file itself is of limited usefulness if relying on it to manually extract information about open windows and their states; and they contain data objects not conventional. These typically link to plist~/Library/Containers/%bundle-id%/Data/Library/Saved Application State/%file%
formats, which makes them un-parsable by common methods. But the command line utilitywhere plutil%bundle-id%
can pick out what's usable.
I wrote these commands in FiSh shell, so it's a little different to Bash, but easily translatable:
cd ~/Library
for f in (find . -iregex '.*\/.*\.savedState\/.*\.plist')
set -l furl (plutil -p "$f" | egrep -io -e '"file:///[^"]+"' | egrep -io -e '/[^/][^"]+')
set -l id (basename -s '.savedState' (printf "$f" | egrep -io -e '[^/]+\.savedState'))
[ -n "$furl" ]; and printf '%s' "$id:"\n\n $furl\n \n\n
end
The output for me is:
com.apple.ScriptEditor2:
/Users/CK/Documents/Scripts/AppleScript/scripts/Download%20Tumblr%20Posts.applescript
/Users/CK/Documents/Scripts/AppleScript/scripts/_.applescript
/Users/CK/Library/Mobile%20Documents/com~apple~ScriptEditor2/Documents/Untitled%202.scpt
com.apple.Automator:
/Users/CK/Library/Mobile%20Documents/com~apple~Automator/Documents/Untitled.workflow
com.luckymarmot.Paw:
/Users/CK/Library/Mobile%20Documents/com~apple~CloudDocs/Paw/Tumblr.paw
Script Editor is currently open with those three documents. The other two applications are closed, but their last open documents were as listed. Thereforefor example, cross-matching the results of this with a list of running applications would be a method to bear in mind.
Howevercom.apple.Preview
, it failed to list a photo image I had open for editing inand its corresponding Affinity. But it did list it when I opened it in%file%
is Previewcom.apple.Preview.savedState
.
This means that ~/Library/Containers
will likely have to be synchronised between the two machines as well.