Given that Safari doesn't have a good, consistent and currently working session manager (that I've found) I decided to build one using Safari's own Bookmark folders. I have macros for saving a session with a description, date and number of tabs as well as a restore macro that brings up a list of sessions for me to select.
Unfortunately, while I thought I had a delete session macro working by parsing Safari's Bookmarks.plist file and deleting the folder with plutil it turns out that just doing that causes problems with iCloud bookmark syncing.
I've been digging through the plist and searching for information on correctly, programmatically killing a bookmark folder but I haven't found any up-to-date solutions.
Does anyone have any suggestions or ideas on how I might accomplish this? I'm open to UI scripting or a more elegant solution. It would just be great to have all of these functions together in a palette I can pull up and handle whatever operation I need to do.
My only idea right now is to open the Safari sidebar, find a specified folder, open a menu and delete it and close the sidebar but I'm not sure how to use AppleScript to find the folder element by name. Is that potentially the best way to go?
Session folders are top level folders, not nested, but they also are guaranteed to be on screen without scrolling if that makes any difference.
I'm not comfortable suggesting a full-scale approach to your goal, but I wonder if targeting the LastSession.plist file will help. It's located in ~/Library/Safari. As I understand it, this file gets updated every time Safari is quit, and is the basis for the History > Reopen All Windows From Last Session menu command.
I have Keyboard Maestro make automatic backups of this file just for "in case of emergency" purposes, but I'm wondering if your more sophisticated goals could be accomplished by targeting this file instead of Bookmarks.plist.
Just in case anyone using Safari is open to using Chrome, I wanted to mention some advantages of Chrome:
Provides direct access to and control of bookmark folders and bookmarks using AppleScript
Has an extension called OneTab that works really great to manage sessions.
Here's an example of one of my Chrome AppleScripts:
Chrome AppleScript
Chrome AppleScript
tell application "Google Chrome"
----------------------------------
if (addDateOpt = "PREFIX") then
set bmTitle to "[" & dateISOStr & "] " & bmTitle
else if (addDateOpt = "SUFFIX") then
set bmTitle to bmTitle & " [" & dateISOStr & "]"
end if
set oBMFolder to bookmark folder "Bookmarks Bar"
--set oBMFolder to bookmark folder (item 1 of bmFolderList) of bookmark folder "Bookmarks Bar"
set chooseBMBol to false
repeat with aBMTitle in bmFolderList
set aBMTitle to contents of aBMTitle
if (aBMTitle = "?") then
if (length of bmFolderPath > 1) then
set bmFolderPath to (text 1 thru -2 of bmFolderPath)
else
set bmFolderPath to ""
end if
set oBMFolder to my chooseBMFolder(oBMFolder, bmFolderPath)
if (oBMFolder ≠ "") then
set bmFolderPath to bmFolderPath & "/" & (title of oBMFolder)
end if
exit repeat
end if
set oBMFolder to bookmark folder (aBMTitle) of oBMFolder
end repeat
if (oBMFolder ≠ "") then
tell oBMFolder
set newBM to make new bookmark item with properties {title:bmTitle, URL:pageURL}
end tell
set scriptResults to "OK " & ¬
"Chrome BookMark was CREATED in Folder: " & LF & bmFolderPath
else
set scriptResults to "[USER CANCELED]" & LF & "A BookMark was NOT Created."
end if
end tell -- "Google Chrome"
Sorry for the delayed reply guys, the week got away with me but I really appreciate your help!
So I've got the entire set of macros working now, I figured out how to use AppleScript to delete the specified session Safari's bookmarks sidebar though it's certainly a compromise doing so much UI scripting. It can be slow if my CPU is otherwise under heavy load (or screen recording), occasionally has a timing error but it's a decent start.
@NaOH Thanks for pointing that out, I had noticed that file but it didn't occur to me that it might be useful. I'm going to investigate LastSession.plist further, it looks like it stays (at least partially) up to date while Safari is running so as long as there's a way to determine which is current and which is previous I could actually make use of it save current sessions out, managed them separately from Safari and then pass the links to Safari any time I want to restore them and eliminate UI scripting altogether.
Assuming I don't discover an iCloud conflict here too — given that sessions are independent to the instance of the browser I hope there won't be — I think this will be how I approach it for version 2.
@nikivi I've tried that in the past and used it for quite a while but it hasn't seemed stable when I've tried to use it recently. Alas, I've switched to Safari Developer Preview as default until Safari 12 is released so I haven't been able to install it and don't want to depend on it if the developer is not planning to update it for the Mac App Store.
I would be very interested in your Alfred workflow if you build one, ideally that's how I'd implement a session manager but I'm not quite there yet.
@JMichaelTX The fact that Chrome has more comprehensive AppleScript support than Safari is unfortunately symptomatic of Apple's neglect of macOS, particularly for power users.
That said, I'm not a fan with Google's approach to tracking and privacy, I definitely don't want to use their browser as my primary.
I'm not a fan of it either, but I've found ways to mitigate these issues to the point where they are no longer a practical concern to me. Everything has its price. The price with most of the Apple apps is poorer UI (IMO) and often poorer scripting support. OTOH, I find the Google products (Browser, Search, Maps) to be best of breed.
But is great that we all have options, so each can choose what he/she prefers.