I\'m making a script, which updates bookmarks on my macOS Safari to always have all of my subscribed subreddits as individual bookmarks in a specific folder. I\'ve gotten to
I never found the AS commands to manage bookmarks in Safari (not in AS dictionary). So I built my own routines to play with Safari bookmark plist file. However, they are subject to unexpected changes made by Apple in the way bookmarks will be handled in future ! up to now , it is still working, but I do not use yet the 10.14
First you must get this plist file to change it. This part must be in your main code. it gives you the patch to your plist file :
set D_Lib to ((path to library folder from user domain) as string) & "Safari"
set SafariPlistFile to D_Lib & ":Bookmarks.plist"
Here are 2 sub-routine to manage bookmarks. The first one checks if a bookmark exists
on Exist_BM(FPlist, BM_Name) -- Search bookmark named BM_Name in Plist file. returns number or 0 if not found. This search is limited to main bar, not sub menus
tell application "System Events"
set numBM to 0
set Main_Bar to property list item "Children" of property list item 2 of property list item "Children" of property list file FPlist
tell Main_Bar
set myBM to every property list item of Main_Bar
repeat with I from 1 to (count of myBM)
set myType to value of property list item "WebBookmarkType" of (item I of myBM)
if (myType = "WebBookmarkTypeLeaf") then
if (value of property list item "title" of property list item "URIDictionary" of (item I of myBM)) = BM_Name then
set numBM to I
exit repeat
end if
end if
end repeat
end tell
end tell
return numBM
end Exist_BM
You can call this handler like bellow :
Set myAndroid to Exist_BM(SafariPlistFile,"r/Android")
if myAndroid >0 then -- set here the code to update : the bookmark already exists
else -- set here the code to add new bookmark "r/Android"
end if
The second handler creates a new bookmark :
on New_BM(FPlist, BM_Name, N_URL) -- create new bookmark at right end side of bookmarks and return its number
tell application "System Events"
set Main_Bar to property list item "Children" of property list item 2 of property list item "Children" of property list file FPlist
set numBM to count of property list item of Main_Bar
tell Main_Bar
set my_UUID to do shell script "uuidgen" -- create unique Apple UID
set myNewBM to make new property list item at the end with properties {kind:record}
tell myNewBM
set URIDict to make new property list item with properties {kind:record, name:"URIDictionary"}
tell URIDict to make new property list item with properties {name:"title", kind:string, value:BM_Name}
make new property list item with properties {name:"URLString", kind:string, value:N_URL}
make new property list item with properties {name:"WebBookmarkType", kind:string, value:"WebBookmarkTypeLeaf"}
make new property list item with properties {name:"WebBookmarkUUID", kind:string, value:my_UUID}
end tell -- myNewBM
end tell
end tell
return (numBM + 1)
end New_BM
I used these routines to add, check and change a bookmark at right side of my bookmarks. In your case, you need to play with bookmark sub menu, and then you have to adjust this code, but the main concept is the same.
To make it easier, I recommend you to start looking your plist file (Library/Safari/Bookmarks.plist) to see its structure when you have your bookmarks in sub menu.
I hope it helps !