The script included below bookmarks files and opens bookmarked files with their default apps. It is a minor rewrite of the script included in post 14 above.
It may be useful to specify the apps that will be used to open files that have a specified extension. To implement this feature, replace the openBookmark handler with the following and change the first line of the handler to the desired file extensions and apps. Some file extensions (e.g. |app|) may need to be surrounded by vertical bars:
on openBookmark(bookmarkData, fileName)
set theApps to {scpt:"Script Editor", txt:"TextEdit"} -- this must contain a record
set theApps to current application's NSDictionary's dictionaryWithDictionary:theApps
set theFile to bookmarkData's objectForKey:fileName
set fileExtension to theFile's pathExtension()'s lowercaseString()
set theApp to theApps's objectForKey:fileExtension
set theWorkSpace to current application's NSWorkspace's sharedWorkspace()
if theApp is missing value then
set fileOpened to (theWorkSpace's openFile:theFile)
else
set fileOpened to (theWorkSpace's openFile:theFile withApplication:theApp)
end if
if fileOpened as boolean is false then deleteOrphanBookmark(bookmarkData, fileName)
delay 0.5
end openBookmark
The script has a flaw of sorts in that the bookmark names are the file names but without an extension. This means that you cannot bookmark both FileOne.txt and FileOne.pdf. To include the file extensions in the bookmark names, replace existing line 1 below with new line 2:
set fileName to (theFile's stringByDeletingPathExtension())'s lastPathComponent()
set fileName to theFile's lastPathComponent()
Please note that the script creates a small plist file in the user’s preference folder.
use framework "AppKit"
use framework "Foundation"
use scripting additions
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:"localizedStandardCompare:")
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 dialogMenu to bookmarkNames's arrayByAddingObjectsFromArray:{"--", "Add a Bookmark", "Delete a Bookmark"}
set dialogResult to (choose from list (dialogMenu as list) with title "File Bookmark" default items dialogDefault)
if dialogResult is false then error number -128
set dialogResult to item 1 of dialogResult
if dialogResult is "Add a Bookmark" then
set {bookmarkData, dialogResult} to addBookmark(bookmarkData, bookmarkNames)
writePlist("bookmarkDataKey", bookmarkData)
else if dialogResult is "Delete a Bookmark" then
set bookmarkData to deleteBookmark(bookmarkData, bookmarkNames)
writePlist("bookmarkDataKey", bookmarkData)
else if dialogResult is "--" then
error number -128
else
openBookmark(bookmarkData, dialogResult)
end if
writePlist("dialogDefaultKey", dialogResult)
end main
on addBookmark(bookmarkData, bookmarkNames)
set theFiles to (choose file with prompt "Select files to bookmark" with multiple selections allowed)
set theFiles to reverse of theFiles
repeat with aFile in theFiles
set theFile to (current application's NSString's stringWithString:(POSIX path of aFile))
set fileName to (theFile's stringByDeletingPathExtension())'s lastPathComponent()
if (bookmarkNames's containsObject:fileName) then
display alert "A bookmark with the name " & quote & (fileName as text) & quote & " already exists and will be skipped"
else
(bookmarkData's setObject:theFile forKey:fileName)
end if
end repeat
return {bookmarkData, fileName}
end addBookmark
on deleteBookmark(bookmarkData, bookmarkNames as list)
if bookmarkNames is {} then
display alert "An error has occurred" message "There are no bookmarks to delete"
error number -128
end if
set deleteItems to (choose from list bookmarkNames with title "File Bookmark" with prompt "Select bookmarks to delete" default items {item 1 of bookmarkNames} with multiple selections allowed)
if deleteItems is false then error number -128
set deleteItems to current application's NSArray's arrayWithArray:deleteItems
(bookmarkData's removeObjectsForKeys:deleteItems)
return bookmarkData
end deleteBookmark
on openBookmark(bookmarkData, fileName)
set theFile to bookmarkData's objectForKey:fileName
set fileOpened to (current application's NSWorkspace's sharedWorkspace())'s openFile:theFile
if fileOpened is false then deleteOrphanBookmark(bookmarkData, fileName)
delay 0.5
end openBookmark
on deleteOrphanBookmark(bookmarkData, fileName)
display alert "An error has occurred" message "The bookmark " & quote & fileName & quote & " could not be opened. Either the file or its designated app could not be found." buttons {"Cancel", "Delete Bookmark"} cancel button 1 default button 2 as critical
(bookmarkData's removeObjectForKey:fileName)
writePlist("bookmarkDataKey", bookmarkData)
writePlist("dialogDefaultKey", "Add a Bookmark")
error number -128
end deleteOrphanBookmark
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()