Monday, July 26, 2021

#1 2018-09-05 12:11:39 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 971

Finder Bookmark Utility

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.

Applescript:

-- 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

Last edited by peavine (2020-10-05 07:39:23 am)


2018 Mac mini - macOS Catalina - Script Debugger 8


Filed under: bookmarks

Offline

 

#2 2018-09-07 12:29:12 pm

Adam Bell
Administrator
From:: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4666

Re: Finder Bookmark Utility

Welcome to Code Exchange, Peavine.

Note that there's an Applescript button on the form for submitting code. Using that for selected AppleScript code encloses the code with a wrapper that makes it easy for readers to open the code in their own Script Editor. I've inserted the appropriate tags in your script.

Adam Bell


Mac mini running 10.14.6, 2011 27" iMac as display.

Offline

 

#3 2018-09-07 03:38:31 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 971

Re: Finder Bookmark Utility

Adam. Thanks for letting my know that and for making the correction. I don't know how I missed that big Applescript button. smile

Peavine


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

#4 2020-10-02 07:22:25 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 971

Re: Finder Bookmark Utility

The following is a companion script designed to work with the script in post 1 above. It automatically adds a bookmark for the folder that is the target of the front finder window (the bookmark name is the folder name). It also sets the added bookmark as the default selection in the bookmark menu.

The operation of the script is simple: run the script and it will add the bookmark and make that bookmark the default selection. This script creates two small text files in the user's preferences folder if they don't exist. There's no reason to run this script until the script in post 1 has been tested and found to be useful.

Applescript:

-- Revised 2020.10.04

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"
   
   try
       tell application "Finder" to set hfsPath to target of Finder window 1 as alias
       set posixPath to POSIX path of hfsPath
   on error
       display alert "Finder Bookmark Helper" message "Finder window not found or target of Finder window cannot be bookmarked." buttons {"OK"} default button 1 as critical
       error number -128
   end try
   
   set AppleScript's text item delimiters to {":"}
   set bookmarkName to text item -2 of (hfsPath as text)
   set AppleScript's text item delimiters to {""}
   
   try
       set bookmarkData to paragraphs of (read POSIX file dataFile)
   on error
       set bookmarkData to {}
   end try
   
   if bookmarkName is in bookmarkData then
       display notification quote & bookmarkName & quote & " bookmark exists" with title "Bookmark Helper"
       writeFile(bookmarkName, settingFile)
       error number -128
   else
       display notification quote & bookmarkName & quote & " bookmark created" with title "Bookmark Helper"
   end if
   
   set AppleScript's text item delimiters to {linefeed}
   set bookmarkData to ({bookmarkName, posixPath} & bookmarkData) as text
   set AppleScript's text item delimiters to {""}
   
   writeFile(bookmarkData, dataFile)
   writeFile(bookmarkName, settingFile)
end main

on writeFile(theText, theFile)
   try
       set openedFile to open for access (POSIX file theFile) with write permission
       set eof of openedFile to 0
       write theText to openedFile
       close access openedFile
   on error
       try
           close access openedFile
       end try
   end try
end writeFile

Last edited by peavine (2020-10-04 07:53:20 am)


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

#5 2021-03-06 09:40:42 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 971

Re: Finder Bookmark Utility

I revised my script to utilize property list files to store bookmark data. The script can be tested by opening and running it in Script Editor. A few comments:

* The script bookmarks the folder that is the target of the front Finder window.
* A divider can be inserted in the bookmark list by selecting "Add a Bookmark" and entering a dash followed by any or no additional characters.
* The script has been tested on Catalina only.
* The script does not work properly when trying to bookmark a folder in a volume on an external drive created by the asr software-restore utility. There is no simple fix for this.

Applescript:

-- Revised 2021.03.14

use framework "AppKit"
use framework "Foundation"
use scripting additions

on main()
   set bookmarkSettingPlist to "com.peavine.FinderBookmarkSetting"
   set dialogDefault to item 1 of readPlist(bookmarkSettingPlist, "Add a Bookmark")
   set bookmarkDataPlist to "com.peavine.FinderBookmarkData"
   set bookmarkData to readPlist(bookmarkDataPlist, {})
   set bookmarkNames to {}
   
   repeat with anItem in bookmarkData
       set end of bookmarkNames to item 1 of anItem
   end repeat
   
   set dialogMenu to bookmarkNames & {"--", "Add a Bookmark", "Delete a Bookmark", "Sort Bookmarks"}
   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
       set bookmarkData to addBookmark(bookmarkData, bookmarkNames, bookmarkSettingPlist)
       writePlist(bookmarkDataPlist, bookmarkData)
   else if dialogResult = "Delete a Bookmark" then
       set bookmarkData to deleteBookmark(bookmarkData, bookmarkNames)
       writePlist(bookmarkDataPlist, bookmarkData)
   else if dialogResult = "Sort Bookmarks" then
       set bookmarkData to sortList(bookmarkData)
       writePlist(bookmarkDataPlist, bookmarkData)
   else if dialogResult begins with "-" then
       error number -128
   else
       changeFolder(bookmarkData, dialogResult)
   end if
   
   writePlist(bookmarkSettingPlist, {dialogResult})
end main

on addBookmark(bookmarkData, bookmarkNames, bookmarkSettingPlist)
   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 target of Finder window cannot be bookmarked")
   end try
   set currentPath to POSIX path of currentPath
   
   set thePath to (current application's NSString's stringWithString:currentPath)'s pathComponents()
   if (count thePath) < 7 then
       set dialogPath to text 1 thru -2 of currentPath
   else
       set dialogPath to current application's NSString's stringWithFormat_("/%@/%@/.../%@/%@", item 2 of thePath, item 3 of thePath, item -3 of thePath, item -2 of thePath)
   end if
   
   display dialog "Bookmark name for " & quote & dialogPath & quote 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
   else if currentName is in bookmarkNames then
       writePlist(bookmarkSettingPlist, {"Add a Bookmark"})
       errorDialog("Add a Bookmark", "A bookmark named " & quote & currentName & quote & " already exists")
   else if currentName begins with "-" then
       set currentPath to currentName
   end if
   
   set beginning of bookmarkData to {currentName, currentPath}
   return bookmarkData
end addBookmark

on deleteBookmark(bookmarkData, bookmarkNames)
   if bookmarkNames = {} then errorDialog("Delete a Bookmark", "There are no bookmarks to delete")
   
   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
   
   repeat with anItem in bookmarkData
       if (item 1 of anItem) is in deleteList then set contents of anItem to missing value
   end repeat
   return lists of bookmarkData
end deleteBookmark

on sortList(theList)
   repeat with i from (count theList) to 2 by -1
       repeat with j from 1 to i - 1
           if item 1 of item j of theList > item 1 of item (j + 1) of theList then
               set {item j of theList, item (j + 1) of theList} to {item (j + 1) of theList, item j of theList}
           end if
       end repeat
   end repeat
   return theList
end sortList

on changeFolder(bookmarkData, dialogResult)
   repeat with anItem in bookmarkData
       if item 1 of anItem = dialogResult then
           try
               set thePath to (POSIX file (item 2 of anItem)) 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
   
   try
       tell application "Finder" to set target of Finder window 1 to thePath
   on error
       set theWorkSpace to current application's NSWorkspace's sharedWorkspace()
       theWorkSpace's openURL:thePath
   end try
end changeFolder

on readPlist(thePlist, theDefault)
   set theDefaults to current application's NSUserDefaults's alloc()'s initWithSuiteName:thePlist
   theDefaults's registerDefaults:{theKey:theDefault}
   return theKey of theDefaults as list
end readPlist

on writePlist(thePlist, theList)
   set theDefaults to current application's NSUserDefaults's alloc()'s initWithSuiteName:thePlist
   set theKey of theDefaults to theList
end writePlist

on errorDialog(textOne, textTwo)
   display alert textOne message textTwo buttons {"OK"} default button 1 as critical
   error number -128
end errorDialog

main()

Last edited by peavine (2021-03-14 08:33:25 am)


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

#6 2021-05-10 07:50:22 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 971

Re: Finder Bookmark Utility

This is a revised version of my script. It uses a dictionary rather than an array to save bookmark data but otherwise is pretty much unchanged.

Applescript:

-- Revised 2021.05.11

use framework "AppKit"
use framework "Foundation"
use scripting additions

on main()
   set preferencesFolder to POSIX path of (path to preferences as text)
   set settingPlist to preferencesFolder & "FinderBookmarkSetting.plist"
   set dataPlist to preferencesFolder & "FinderBookmarkData.plist"
   
   try
       set dialogDefault to settingOne of readPlist(settingPlist) as text
   on error
       set dialogDefault to "Add a Bookmark"
   end try
   
   try
       set bookmarkData to readPlist(dataPlist)
       set bookmarkNames to (bookmarkData's allKeys()'s sortedArrayUsingSelector:"caseInsensitiveCompare:") as list
   on error
       set bookmarkData to (current application's NSMutableDictionary's new())
       set bookmarkNames to {}
   end try
   
   set dialogMenu to bookmarkNames & {"--", "Add a Bookmark", "Delete a Bookmark"}
   set dialogResult to (choose from list dialogMenu with title "Finder Bookmark" default items dialogDefault)
   if dialogResult = false then error number -128
   set dialogResult to item 1 of dialogResult
   
   if dialogResult = "Add a Bookmark" then
       set {bookmarkData, currentName} to addBookmark(bookmarkData, bookmarkNames, settingPlist)
       writePlist(dataPlist, bookmarkData)
       set dialogResult to currentName
   else if dialogResult = "Delete a Bookmark" then
       set bookmarkData to deleteBookmark(bookmarkData, bookmarkNames)
       writePlist(dataPlist, bookmarkData)
   else if dialogResult begins with "-" then
       error number -128
   else
       changeFolder(bookmarkData, dialogResult)
   end if
   
   set theSetting to current application's NSDictionary's dictionaryWithDictionary:{settingOne:dialogResult}
   writePlist(settingPlist, theSetting)
end main

on addBookmark(bookmarkData, bookmarkNames, settingPlist)
   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", "A Finder window was not found or the target of the front Finder window cannot be bookmarked")
   end try
   set currentPath to POSIX path of currentPath
   
   set thePath to (current application's NSString's stringWithString:currentPath)'s pathComponents()
   if (count thePath) < 7 then
       set dialogPath to text 1 thru -2 of currentPath
   else
       set dialogPath to current application's NSString's stringWithFormat_("/%@/%@/.../%@/%@", item 2 of thePath, item 3 of thePath, item -3 of thePath, item -2 of thePath) as text
   end if
   
   set currentName to text returned of (display dialog "Bookmark name for " & quote & dialogPath & quote default answer currentName buttons {"Cancel", "OK"} default button 2 cancel button 1 with title "Finder Bookmark")
   if currentName = "" then error number -128
   
   if currentName is in bookmarkNames then
       set theSetting to current application's NSDictionary's dictionaryWithDictionary:{settingOne:currentName}
       writePlist(settingPlist, theSetting)
       errorDialog("Add a Bookmark", "A bookmark named " & quote & currentName & quote & " already exists")
   end if
   
   bookmarkData's setObject:currentPath forKey:currentName
   return {bookmarkData, currentName}
end addBookmark

on deleteBookmark(bookmarkData, bookmarkNames)
   if bookmarkNames = {} then errorDialog("Delete a Bookmark", "There are no bookmarks to delete")
   
   set deleteItems to (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 deleteItems = false then error number -128
   
   set deleteItems to current application's NSArray's arrayWithArray:deleteItems
   (bookmarkData's removeObjectsForKeys:deleteItems)
   return bookmarkData
end deleteBookmark

on changeFolder(bookmarkData, dialogResult)
   set thePath to (bookmarkData's valueForKey:dialogResult) as text
   
   try
       set theAlias to POSIX file thePath as alias
   on error
       errorDialog("Go To Bookmark", "The folder named " & quote & dialogResult & quote & " is offline or otherwise not available")
   end try
   
   try
       tell application "Finder" to set target of Finder window 1 to theAlias
   on error
       set theWorkSpace to current application's NSWorkspace's sharedWorkspace()
       theWorkSpace's openURL:theAlias
   end try
end changeFolder

on readPlist(thePath)
   return (current application's NSMutableDictionary's dictionaryWithContentsOfFile:thePath)
end readPlist

on writePlist(thePath, theDictionary)
   theDictionary's writeToFile:thePath atomically:true
end writePlist

on errorDialog(textOne, textTwo)
   display alert textOne message textTwo buttons {"OK"} default button 1 as critical
   error number -128
end errorDialog

main()

Last edited by peavine (2021-05-11 05:04:52 pm)


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

#7 2021-07-02 12:02:05 am

One208
Member
Registered: 2021-03-15
Posts: 17

Re: Finder Bookmark Utility

Hi Peavine

Thank you 4 wonderful script, I quite like the idea of having frequently used folders at hand, with ability to add/ remove from the list

Just wondered if its possible to do a similar list with frequently used documents/ Files. Selecting them in list → opens them in default application

Cheers

Last edited by One208 (2021-07-02 12:11:44 am)

Offline

 

#8 2021-07-02 07:23:43 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 971

Re: Finder Bookmark Utility

One208 wrote:

Just wondered if its possible to do a similar list with frequently used documents/ Files. Selecting them in list → opens them in default application



One208. I'm happy to hear that you like my bookmark script, and thanks for the suggestion, which would not be difficult to implement but would take a bit of time to write and test. I'll work on that after I finish another project.

Last edited by peavine (2021-07-02 08:12:10 am)


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

#9 2021-07-13 05:06:10 pm

One208
Member
Registered: 2021-03-15
Posts: 17

Re: Finder Bookmark Utility

Hi Peavine
Thank you for ur script, I have modified a few lines for it to bookmark Files and open them in default application
Please suggest if following cud be achieved
- Select Multiple Files & Bookmark them
- Prevent Folders getting Bookmarked
Cheers

Applescript:


use framework "AppKit"
use framework "Foundation"
use scripting additions

on main()
   set preferencesFolder to POSIX path of (path to preferences as text)
   set settingPlist to preferencesFolder & "FileBookmarkSetting.plist"
   set dataPlist to preferencesFolder & "FileBookmarkData.plist"
   
   try
       set dialogDefault to settingOne of readPlist(settingPlist) as text
   on error
       set dialogDefault to "Add a FileBookmark"
   end try
   
   try
       set bookmarkData to readPlist(dataPlist)
       set bookmarkNames to (bookmarkData's allKeys()'s sortedArrayUsingSelector:"caseInsensitiveCompare:") as list
   on error
       set bookmarkData to (current application's NSMutableDictionary's new())
       set bookmarkNames to {}
   end try
   
   set dialogMenu to bookmarkNames & {"--", "Add a FileBookmark", "Delete a FileBookmark"}
   set dialogResult to (choose from list dialogMenu with title "File Bookmark" default items dialogDefault)
   if dialogResult = false then error number -128
   set dialogResult to item 1 of dialogResult
   
   if dialogResult = "Add a FileBookmark" then
       set {bookmarkData, currentName} to addBookmark(bookmarkData, bookmarkNames, settingPlist)
       writePlist(dataPlist, bookmarkData)
       set dialogResult to currentName
   else if dialogResult = "Delete a FileBookmark" then
       set bookmarkData to deleteBookmark(bookmarkData, bookmarkNames)
       writePlist(dataPlist, bookmarkData)
   else if dialogResult begins with "-" then
       error number -128
   else
       changeFolder(bookmarkData, dialogResult)
   end if
   
   set theSetting to current application's NSDictionary's dictionaryWithDictionary:{settingOne:dialogResult}
   writePlist(settingPlist, theSetting)
end main

on addBookmark(bookmarkData, bookmarkNames, settingPlist)
   try
       tell application "Finder"
           set currentPath to selection as alias
           set currentName to name of currentPath
           ##            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", "A Selected File was not found or the File cannot be bookmarked")
   end try
   set currentPath to POSIX path of currentPath
   
   set thePath to (current application's NSString's stringWithString:currentPath)'s pathComponents()
   if (count thePath) < 7 then
       set dialogPath to text 1 thru -2 of currentPath
   else
       set dialogPath to current application's NSString's stringWithFormat_("/%@/%@/.../%@/%@", item 2 of thePath, item 3 of thePath, item -3 of thePath, item -2 of thePath) as text
   end if
   
   set currentName to text returned of (display dialog "Bookmark name for " & quote & dialogPath & quote default answer currentName buttons {"Cancel", "OK"} default button 2 cancel button 1 with title "File Bookmark")
   if currentName = "" then error number -128
   
   if currentName is in bookmarkNames then
       set theSetting to current application's NSDictionary's dictionaryWithDictionary:{settingOne:currentName}
       writePlist(settingPlist, theSetting)
       errorDialog("Add a Bookmark", "A bookmark named " & quote & currentName & quote & " already exists")
   end if
   
   bookmarkData's setObject:currentPath forKey:currentName
   return {bookmarkData, currentName}
end addBookmark

on deleteBookmark(bookmarkData, bookmarkNames)
   if bookmarkNames = {} then errorDialog("Delete a Bookmark", "There are no bookmarks to delete")
   
   set deleteItems to (choose from list bookmarkNames with title "File Bookmark" with prompt "Select File bookmarks to delete:" default items {item 1 of bookmarkNames} with multiple selections allowed)
   if deleteItems = false then error number -128
   
   set deleteItems to current application's NSArray's arrayWithArray:deleteItems
   (bookmarkData's removeObjectsForKeys:deleteItems)
   return bookmarkData
end deleteBookmark

on changeFolder(bookmarkData, dialogResult)
   set thePath to (bookmarkData's valueForKey:dialogResult) as text
   
   try
       set theAlias to POSIX file thePath as alias
   on error
       errorDialog("Go To Bookmark", "The File named " & quote & dialogResult & quote & " is offline or otherwise not available")
   end try
   
   try
       -- Uncommnet to switch between New window v/s ReUse Front window CJ Mod
       # tell application "Finder" to set target of Finder window 1 to theAlias -- uses foremost finder window to display Selected folder
       # tell application "Finder" to make new window to theAlias -- Opens New Finder window
       
       tell application "Finder" to open theAlias -- Opens Document (need to be POSIX path as AppleScript cannot coerce to an alias or file object acceptable to the Finder)
   on error
       set theWorkSpace to current application's NSWorkspace's sharedWorkspace()
       theWorkSpace's openURL:theAlias
   end try
end changeFolder

on readPlist(thePath)
   return (current application's NSMutableDictionary's dictionaryWithContentsOfFile:thePath)
end readPlist

on writePlist(thePath, theDictionary)
   theDictionary's writeToFile:thePath atomically:true
end writePlist

on errorDialog(textOne, textTwo)
   display alert textOne message textTwo buttons {"OK"} default button 1 as critical
   error number -128
end errorDialog

main()

Last edited by One208 (2021-07-13 05:38:03 pm)

Offline

 

#10 2021-07-13 07:20:12 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 971

Re: Finder Bookmark Utility

One208. If you want to bookmark multiple files at one time, my inclination would be to skip the dialog that prompts for a bookmark name and instead to utilize the file name as the bookmark name. Then, the script could fairly quickly loop through each selected file and add the file name and path to the bookmarkData dictionary. Just as a rough demonstration:

Applescript:

tell application "Finder" to set theSelectedFiles to selection as alias list
repeat with aFile in theSelectedFiles
-- the code which adds the file name and path to the dictionary
end repeat

As regards identifying a folder, there are many options, but perhaps the simplest is to use Finder's kind property. For example

Applescript:

tell application "Finder"
   set theFiles to selection as alias list
   set theKind to kind of item 1 of theFiles --> returns "Folder" for a folder
end tell

I tested your script and it seems to work well.


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

#11 2021-07-17 09:54:03 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 971

Re: Finder Bookmark Utility

This script allows the user to bookmark and then open files selected in a Finder window and on the desktop. Its operation is straightforward, although it should be noted:

* The script does not allow the user to bookmark folders or package files.

* The script opens the bookmarked files with the default app.

* When adding a bookmark, an existing bookmark with the same name is overwritten.

Applescript:

-- Revised 2021.07.21

use framework "AppKit"
use framework "Foundation"
use scripting additions

on main()
   set preferencesFolder to POSIX path of (path to preferences as text)
   set settingPlist to preferencesFolder & "FinderBookmarkFileSetting.plist"
   set dataPlist to preferencesFolder & "FinderBookmarkFileData.plist"
   
   set dialogDefault to readPlist(settingPlist)
   if dialogDefault = (missing value) then
       set dialogDefault to "Add a Bookmark"
   else
       set dialogDefault to (settingOne of dialogDefault) as list
   end if
   
   set bookmarkData to readPlist(dataPlist)
   if bookmarkData = (missing value) then
       set bookmarkData to (current application's NSMutableDictionary's new())
       set bookmarkNames to {}
   else
       set bookmarkNames to (bookmarkData's allKeys()'s sortedArrayUsingSelector:"caseInsensitiveCompare:") as list
   end if
   
   set dialogMenu to bookmarkNames & {"--", "Add a Bookmark", "Delete a Bookmark"}
   set dialogResult to (choose from list dialogMenu with title "Finder File Bookmark" default items dialogDefault with multiple selections allowed)
   if dialogResult = false then error number -128
   if (count dialogResult) > 1 then
       if "Add a Bookmark" is in dialogResult or "Delete a Bookmark" is in dialogResult or "--" is in dialogResult then
           errorDialog("Conflicting dialog items were selected")
       end if
   end if
   
   if dialogResult = {"Add a Bookmark"} then
       set bookmarkData to addBookmark(bookmarkData)
       writePlist(dataPlist, bookmarkData)
   else if dialogResult = {"Delete a Bookmark"} then
       set bookmarkData to deleteBookmark(bookmarkData, bookmarkNames)
       writePlist(dataPlist, bookmarkData)
   else if dialogResult = {"--"} then
       error number -128
   else
       openFiles(bookmarkData, dialogResult, dataPlist)
       if result = false then set dialogResult to "Add a Bookmark"
   end if
   
   delay 0.5
   
   set theSetting to current application's NSDictionary's dictionaryWithDictionary:{settingOne:dialogResult}
   writePlist(settingPlist, theSetting)
end main

on addBookmark(bookmarkData)
   try
       tell application "Finder" to set selectedFiles to selection as alias list
   on error
       errorDialog("An item selected in a Finder window or on the desktop cannot be bookmarked")
   end try
   if (count selectedFiles) = 0 then errorDialog("A file must be selected in a Finder window or on the desktop")
   
   repeat with aFile in selectedFiles
       set aFilePath to (current application's NSString's stringWithString:(POSIX path of aFile))
       set aFileName to aFilePath's lastPathComponent() as text
       
       set aFileURL to (current application's |NSURL|'s fileURLWithPath:aFilePath)
       set {theResult, theValue} to (aFileURL's getResourceValue:(reference) forKey:(current application's NSURLIsDirectoryKey) |error|:(missing value))
       if (theValue as boolean) = true then errorDialog("An item selected in a Finder window or on the desktop appears to be a folder or package file and cannot be bookmarked")
       
       set aFileName to text returned of (display dialog "Enter a bookmark name for:" default answer aFileName with icon note)
       if aFileName = "" then errorDialog("A bookmark name was not entered in the dialog")
       
       (bookmarkData's setObject:aFilePath forKey:aFileName)
   end repeat
   
   return bookmarkData
end addBookmark

on deleteBookmark(bookmarkData, bookmarkNames)
   if bookmarkNames = {} then errorDialog("There are no bookmarks to delete")
   
   set deleteItems to (choose from list bookmarkNames with title "Finder File Bookmark" with prompt "Select bookmarks to delete:" default items {item 1 of bookmarkNames} with multiple selections allowed)
   if deleteItems = false then error number -128
   
   set deleteItems to current application's NSArray's arrayWithArray:deleteItems
   (bookmarkData's removeObjectsForKeys:deleteItems)
   return bookmarkData
end deleteBookmark

on openFiles(bookmarkData, fileNames, dataPlist)
   set theFilesToOpen to {}
   set fileManager to current application's NSFileManager's defaultManager()
   repeat with aFileName in fileNames
       set aFilePath to (bookmarkData's valueForKey:aFileName) as text
       if not (fileManager's fileExistsAtPath:aFilePath) then
           removeOrphanBookmark(bookmarkData, aFileName, dataPlist)
           return false
       end if
       set end of theFilesToOpen to quoted form of aFilePath & space
   end repeat
   
   do shell script "open " & theFilesToOpen
   return true
end openFiles

on removeOrphanBookmark(bookmarkData, theBookmark, dataPlist)
   display alert "An error has occurred" message "The file referenced by the bookmark " & quote & theBookmark & quote & " could not be found" buttons {"Stop", "Remove Bookmark"} default button 1 as critical
   if button returned of result = "Stop" then
       error number -128
   else
       (bookmarkData's removeObjectForKey:theBookmark)
       writePlist(dataPlist, bookmarkData)
   end if
end removeOrphanBookmark

on readPlist(thePath)
   return (current application's NSMutableDictionary's dictionaryWithContentsOfFile:thePath)
end readPlist

on writePlist(thePath, theDictionary)
   theDictionary's writeToFile:thePath atomically:true
end writePlist

on errorDialog(messageText)
   display alert "An error has occurred" message messageText buttons {"Stop"} default button 1 as critical
   error number -128
end errorDialog

main()

Last edited by peavine (2021-07-21 07:51:55 am)


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

#12 2021-07-19 08:40:06 am

xjonnie
Member
Registered: 2005-05-09
Posts: 20

Re: Finder Bookmark Utility

The previous 2 posts don't work.

Can’t get settingOne of missing value.
From this line
"set dialogResult to (choose from list dialogMenu with title "Finder File Bookmark" default items dialogDefault with multiple selections allowed)"

However this line is the problem
"try
        set dialogDefault to settingOne of readPlist(settingPlist) as list
    on error
        set dialogDefault to "Add a Bookmark"
    end try"

The first time running this, no plist exists but it does not trigger the error.
I guess the "as list" hides the error from AppleScript. not good

Change to "as text" then it will be detected.

Would probably be better to test for "missing value"??

Also: several lines down
if "Add a Bookmark" is in dialogResult or "Delete a Bookark" is in dialogResult or "--" is in dialogResult then"

"Bookark" should be "Bookmark"

Offline

 

#13 2021-07-19 10:00:27 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 971

Re: Finder Bookmark Utility

Thanks xjonnie for testing my script and reporting the errors. I've fixed both errors in the script in post 11 above.

Last edited by peavine (2021-07-20 06:57:49 am)


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)