EDIT JULY 28, 2021. This thread contains two basic scripts. The first bookmarks folders and opens a bookmarked folder in a Finder window. The most recent version of this script is in post 6. The second script bookmarks files and opens a bookmarked file in the default app. The most recent version is in post 11. The operation of these scripts require no real explanation, and, to test either script, simply open it in a script editor and run. END EDIT
This is a bookmark utility for the Finder. To use the script, first save it as a script or application and then run the script with the Finder open and select “Add a Bookmark”. This creates two text files, which contain the bookmark data, in the ~/Library/Preferences folder. A bookmark name that begins with a dash is treated as a menu divider only; the operation of the utility is otherwise pretty straightforward.
I run the script by way of a keyboard shortcut utilizing the Fastscripts utility. A simpler but perhaps less convenient method is to save the script as an application and then to Command-drag the application to the toolbar, which will create an icon. You may have to set permissions in System Preferences if the script is run as an application.
-- Revised 2020.10.05
main()
on main()
set preferencePath to POSIX path of (path to preferences)
set dataFile to preferencePath & "Bookmark Data.txt"
set settingFile to preferencePath & "Bookmark Setting.txt"
set {bookmarkNames, bookmarkPaths} to readDataFile(dataFile)
set dialogDefault to readSettingFile(settingFile)
if bookmarkNames = {} then
set dialogMenu to {"Add a Bookmark", "Edit Bookmark File"}
set dialogDefault to "Add a Bookmark"
else
set dialogMenu to bookmarkNames & {"--", "Add a Bookmark", "Delete a Bookmark", "Edit Bookmark File"}
end if
choose from list dialogMenu with title "Finder Bookmark" default items dialogDefault
if result = false then
error number -128
else
set dialogResult to result as text
end if
if dialogResult = "Add a Bookmark" then
addBookmark(bookmarkNames, bookmarkPaths, settingFile)
set {bookmarkNames, bookmarkPaths} to result
writeDataFile(bookmarkNames, bookmarkPaths, dataFile)
writeSettingFile(item 1 of bookmarkNames, settingFile)
else if dialogResult = "Delete a Bookmark" then
deleteBookmark(bookmarkNames, bookmarkPaths)
set {bookmarkNames, bookmarkPaths} to result
writeDataFile(bookmarkNames, bookmarkPaths, dataFile)
writeSettingFile("Delete a Bookmark", settingFile)
else if dialogResult = "Edit Bookmark File" then
editBookmarkFile(dataFile)
writeSettingFile("Edit Bookmark File", settingFile)
else if dialogResult begins with "-" then
error number -128
else
changeFolder(bookmarkNames, bookmarkPaths, dialogResult)
writeSettingFile(dialogResult, settingFile)
end if
end main
on readDataFile(dataFile)
set bookmarkNames to {}
set bookmarkPaths to {}
try
set openedFile to paragraphs of (read POSIX file dataFile)
repeat with i from 1 to (count openedFile) by 2
if item i of openedFile > "" then
set end of bookmarkNames to item i of openedFile
set end of bookmarkPaths to item (i + 1) of openedFile
else
exit repeat
end if
end repeat
end try
return {bookmarkNames, bookmarkPaths}
end readDataFile
on readSettingFile(settingFile)
try
set theText to paragraph 1 of (read POSIX file settingFile)
on error
set theText to "Add a Bookmark"
end try
return theText
end readSettingFile
on addBookmark(bookmarkNames, bookmarkPaths, settingFile)
try
tell application "Finder"
set currentName to name of Finder window 1
set currentPath to target of Finder window 1 as alias
end tell
on error
errorDialog("Add a Bookmark", "Finder window not found or the target of Finder window cannot be bookmarked")
end try
set currentPath to POSIX path of currentPath
set text item delimiters to {"/"}
set pathList to text items of currentPath
if (count pathList) ≤ 6 then
set dialogPath to currentPath
else
set dialogPath to (items 1 thru 3 of pathList & "..." & items -3 thru -1 of pathList) as text
end if
set text item delimiters to {""}
display dialog "Bookmark name for \"" & dialogPath & "\"" default answer currentName buttons {"Cancel", "OK"} default button 2 cancel button 1 with title "Finder Bookmark"
set currentName to text returned of result
if currentName = "" then error number -128
if currentName is in bookmarkNames then
writeSettingFile("Add a bookmark", settingFile)
errorDialog("Add a Bookmark", "A bookmark named " & quote & currentName & quote & " already exists")
end if
if currentName begins with "-" then set currentPath to currentName
set beginning of bookmarkNames to currentName
set beginning of bookmarkPaths to currentPath
return {bookmarkNames, bookmarkPaths}
end addBookmark
on deleteBookmark(bookmarkNames, bookmarkPaths)
choose from list bookmarkNames with title "Finder Bookmark" with prompt "Select bookmarks to delete:" default items {item 1 of bookmarkNames} with multiple selections allowed
if result = false then
error number -128
else
set deleteList to result
end if
set nameTemp to {}
set pathTemp to {}
repeat with i from 1 to (count bookmarkNames)
if item i of bookmarkNames is not in deleteList then
set end of nameTemp to item i of bookmarkNames
set end of pathTemp to item i of bookmarkPaths
end if
end repeat
return {nameTemp, pathTemp}
end deleteBookmark
on editBookmarkFile(bookmarkFile)
try
do shell script "open -e " & quoted form of bookmarkFile
delay 0.3
on error
errorDialog("Edit Bookmark File", "The bookmark file could not be found. Please rerun this script and select " & quote & "Add a Bookmark" & quote)
end try
end editBookmarkFile
on changeFolder(bookmarkNames, bookmarkPaths, dialogResult)
repeat with i from 1 to (count bookmarkNames)
if item i of bookmarkNames = dialogResult then
try
set thePath to (POSIX file (item i of bookmarkPaths)) as alias
exit repeat
on error
errorDialog("Change Finder Folder", "The folder named " & quote & dialogResult & quote & " is offline or otherwise not available")
end try
end if
end repeat
tell application "Finder"
try
set target of Finder window 1 to thePath
on error
open thePath
end try
end tell
end changeFolder
on writeDataFile(bookmarkNames, bookmarkPaths, dataFile)
set mergedData to {}
repeat with i from 1 to (count bookmarkNames)
set end of mergedData to (item i of bookmarkNames)
set end of mergedData to (item i of bookmarkPaths)
end repeat
set AppleScript's text item delimiters to {linefeed}
set mergedData to mergedData as text
set AppleScript's text item delimiters to {""}
try
set openedFile to open for access (POSIX file dataFile) with write permission
set eof of openedFile to 0
write mergedData to openedFile
close access openedFile
on error
try
close access openedFile
end try
end try
end writeDataFile
on writeSettingFile(dialogDefault, settingFile)
try
set openedFile to open for access (POSIX file settingFile) with write permission
set eof of openedFile to 0
write dialogDefault to openedFile
close access openedFile
on error
try
close access openedFile
end try
end try
end writeSettingFile
on errorDialog(textOne, textTwo)
display alert textOne message textTwo buttons {"OK"} default button 1 as critical
error number -128
end errorDialog