The following script bookmarks and opens files and is functionally similar to the script immediately above. The primary difference is that the new script uses Shane’s Myriad Tables script library. It should be noted that an AppleScript bug can cause an error to be returned when Myriad Tables is used, although the script has worked without issue on my Sonoma computer.
-- revised 2024.02.27
use framework "AppKit"
use framework "Foundation"
use scripting additions
use script "Myriad Tables Lib" version "1.0.13"
on main()
try
set dialogDefault to readPlist("dialogDefaultKey") as text
set bookmarkData to readPlist("bookmarkDataKey")'s mutableCopy()
set bookmarkNames to (bookmarkData's allKeys()'s sortedArrayUsingSelector:"caseInsensitiveCompare:")
on error
set dialogDefault to "Add a Bookmark"
set bookmarkData to current application's NSMutableDictionary's new()
set bookmarkNames to current application's NSArray's arrayWithArray:{}
end try
set {theSelection, theButton} to showDialog(dialogDefault, bookmarkNames)
if theButton is 2 then -- delete button selected
set bookmarkData to deleteBookmark(bookmarkData, theSelection)
writePlist("bookmarkDataKey", bookmarkData)
set theSelection to "Add a Bookmark"
else if theSelection is "Add a Bookmark" then
set {bookmarkData, theSelection} to addBookmark(bookmarkData, bookmarkNames)
writePlist("bookmarkDataKey", bookmarkData)
else if theSelection is "--" then
error number -128
else
openBookmark(bookmarkData, theSelection)
end if
writePlist("dialogDefaultKey", theSelection)
end main
on showDialog(dialogDefault, bookmarkNames)
set dialogList to bookmarkNames's arrayByAddingObjectsFromArray:{"--", "Add a Bookmark"}
set rowNumber to (dialogList's indexOfObject:dialogDefault) + 1
set theTable to make new table with data (dialogList as list) with prompt "Make a selection:" with title "File Bookmark" initially selected rows {rowNumber} with double click means OK
modify table theTable OK button name "Select" extra button name "Delete" with alternate backgrounds
set theValues to display table theTable
return {item 1 of values selected of theValues, button number of theValues}
end showDialog
on deleteBookmark(bookmarkData, deleteItem)
if bookmarkData's |count|() is 0 then error number -128
bookmarkData's removeObjectForKey:deleteItem
return bookmarkData
end deleteBookmark
on addBookmark(bookmarkData, bookmarkNames)
set theFile to (choose file with prompt "Select a file to bookmark")
set theFile to (current application's NSString's stringWithString:(POSIX path of theFile))
set fileName to (theFile's stringByDeletingPathExtension())'s lastPathComponent()
if (bookmarkNames's containsObject:fileName) then
display alert "An error has occurred" message "A bookmark with the name " & quote & (fileName as text) & quote & " already exists"
error number -128
else
(bookmarkData's setObject:theFile forKey:fileName)
end if
return {bookmarkData, fileName}
end addBookmark
on openBookmark(bookmarkData, fileName)
set theWorkspace to current application's NSWorkspace's sharedWorkspace()
set theFile to current application's |NSURL|'s fileURLWithPath:(bookmarkData's objectForKey:fileName)
set fileSuccess to theWorkspace's openURL:theFile
if fileSuccess is false then display alert "An error has occurred" message "The file " & quote & fileName & quote & " could not be opened"
delay 0.5
end openBookmark
on readPlist(theKey)
set theDefaults to current application's NSUserDefaults's alloc()'s initWithSuiteName:"com.peavine.FileBookmark"
return theDefaults's objectForKey:theKey
end readPlist
on writePlist(theKey, theValue)
set theDefaults to current application's NSUserDefaults's alloc()'s initWithSuiteName:"com.peavine.FileBookmark"
theDefaults's setObject:theValue forKey:theKey
end writePlist
main()