Monday, May 20, 2019

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

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

Finder Bookmark Utility

This is a bookmark utility for the Finder. To use the script, 1) save it as a script or application; 2) modify the paths at the top of the script for two bookmark and setting text files; 3) run the script with the Finder open and select "Add a Bookmark", which should create the necessary text files. A bookmark name that begins with a tilde or 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:


--Reviewed 05.05.19.

--Set global variables.
global bookmarkDataFile, bookmarkSettingsFile, bookmarkNames, bookmarkPaths

--MAIN--

--Start handler.
on run
   
   --Set variables to paths to bookmark data and settings files.
   set bookmarkDataFile to "/Users/bob/Scripts/Preferences/Bookmark Data.txt"
   set bookmarkSettingsFile to "/Users/bob/Scripts/Preferences/Bookmark Settings.txt"
   
   --Set variable to menu-divider characters.
   set menuDividers to {"-", "~"}
   
   --Read bookmark data and settings files.
   readBookmarkFiles()
   set dialogDefault to result
   
   --Set variable to menu for dialog.
   if bookmarkNames = {} then
       set dialogMenu to {"Add a Bookmark", "Edit Bookmark File"}
   else
       set dialogMenu to bookmarkNames & ¬
           {"Add a Bookmark", "Delete a Bookmark", "Edit Bookmark File"}
   end if
   
   --Show dialog.
   choose from list dialogMenu with title "Finder Bookmark" with prompt ¬
       "Select one of the following:" default items dialogDefault
   
   --Set variable to dialog result.
   if result = false then
       error number -128
   else
       set dialogResult to result as text
   end if
   
   --Add a bookmark.
   if dialogResult = "Add a Bookmark" then
       addBookmark(menuDividers)
       writeDataFile()
       writeSettingsFile(item 1 of bookmarkNames)
       
       --Delete a bookmark.
   else if dialogResult = "Delete a Bookmark" then
       deleteBookmark()
       writeDataFile()
       writeSettingsFile("Delete a Bookmark")
       
       --Edit bookmark file.
   else if dialogResult = "Edit Bookmark File" then
       editBookmarkFile()
       writeSettingsFile("Edit Bookmark File")
       
       --Menu divider.
   else if text 1 of dialogResult is in menuDividers then
       writeSettingsFile(dialogResult)
       
       --Change to bookmarked folder.
   else
       changeFolder(dialogResult)
       writeSettingsFile(dialogResult)
   end if
   
   --End handler.    
end run

--READ BOOKMARK FILES--

--Start handler.
on readBookmarkFiles()
   
   --Read bookmark settings file and set variable to dialog default.
   try
       set dialogDefault to paragraph 1 of (read POSIX file bookmarkSettingsFile)
   on error
       set dialogDefault to "Add a Bookmark"
   end try
   
   --Initialize variables for bookmark names and paths.
   set bookmarkNames to {}
   set bookmarkPaths to {}
   
   --Read bookmark data file.
   try
       set openedFile to paragraphs of (read POSIX file bookmarkDataFile)
   on error
       return "Add a Bookmark"
   end try
   
   --Set variables to bookmark names and paths.
   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
       end if
   end repeat
   
   --Return dialog default.
   return dialogDefault
   
   --End handler
end readBookmarkFiles

--ADD A BOOKMARK--

--Start handler.
on addBookmark(menuDividers)
   
   --Set variables to name of, and path to, target of front Finder window.
   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("Finder window not found or target of Finder window cannot be bookmarked.")
   end try
   
   --Set variable to target of front Finder window with POSIX path.
   set currentPath to POSIX path of currentPath
   
   --Set variable to path to target of front Finder window for dialog.
   set AppleScript's 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 AppleScript's text item delimiters to {""}
   
   --Prompt for bookmark name.
   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
   
   --Notify if bookmark name not entered.
   if currentName = "" then
       errorDialog("Bookmark name not entered.")
   end if
   
   --Notify if bookmark name already in use.
   if currentName is in bookmarkNames then
       writeSettingsFile("Add a bookmark")
       errorDialog("Bookmark name already in use.")
   end if
   
   --Set path to menu divider if menu divider entered.
   if text 1 of currentName is in menuDividers then
       set currentPath to currentName
   end if
   
   --Add new bookmark to existing bookmarks.
   set beginning of bookmarkNames to currentName
   set beginning of bookmarkPaths to currentPath
   
   --End handler.
end addBookmark

--DELETE A BOOKMARK--

--Start handler.
on deleteBookmark()
   
   --Show dialog.
   choose from list bookmarkNames with title "Finder Bookmark" with prompt ¬
       "Select bookmarks to delete:" default items {item 1 of bookmarkNames} ¬
       with multiple selections allowed
   
   --Set variable to dialog result.
   if result = false then
       error number -128
   else
       set deleteList to result
   end if
   
   --Set temporary variables to retained bookmark names and paths.
   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
   
   --Set variables to retained bookmark names and paths.
   set bookmarkNames to nameTemp
   set bookmarkPaths to pathTemp
   
   --End handler.
end deleteBookmark

--EDIT BOOKMARK FILE--

--Start handler.
on editBookmarkFile()
   
   --Set variable to bookmark file.
   try
       set bookmarkDataFile to (POSIX file bookmarkDataFile) as alias
   on error
       errorDialog("The bookmark file could not be found.")
   end try
   
   --Open bookmark file.    
   tell application "TextEdit"
       open bookmarkDataFile
       activate
   end tell
   
   --End handler
end editBookmarkFile

--CHANGE FOLDER--

--Start handler.
on changeFolder(dialogResult)
   
   --Set variable to path to selected bookmark.
   repeat with i from 1 to (count bookmarkNames)
       if item i of bookmarkNames = dialogResult then
           try
               set bookmarkPath to (POSIX file (item i of bookmarkPaths)) as alias
               exit repeat
           on error
               errorDialog("Bookmarked folder is offline or otherwise not available.")
           end try
       end if
   end repeat
   
   --Change Finder window to selected bookmark.
   tell application "Finder"
       try
           set target of Finder window 1 to bookmarkPath
       on error
           open bookmarkPath
       end try
   end tell
   
   --End handler.
end changeFolder

--WRITE DATA FILE--

--Start handler.
on writeDataFile()
   
   --Merge bookmark names and paths.
   set mergedBookmarks to {}
   repeat with i from 1 to (count bookmarkNames)
       set end of mergedBookmarks to (item i of bookmarkNames) & return
       set end of mergedBookmarks to (item i of bookmarkPaths) & return
   end repeat
   set mergedBookmarks to mergedBookmarks as text
   
   --Write bookmark names and paths to file.
   try
       set openedFile to open for access (POSIX file bookmarkDataFile) with write permission
       set eof of openedFile to 0
       write mergedBookmarks to openedFile
       close access openedFile
   on error
       try
           close access openedFile
       end try
   end try
   
   --End handler.
end writeDataFile

--WRITE SETTINGS FILE--

--Start handler.
on writeSettingsFile(dialogDefault)
   
   --Write dialog selection to file.
   try
       set openedFile to open for access (POSIX file bookmarkSettingsFile) 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 handler.
end writeSettingsFile

--ERROR DIALOG--

--Display error dialog.
on errorDialog(dialogText)
   display dialog dialogText buttons {"OK"} default button 1 cancel button 1 ¬
       with title "Finder Bookmark" with icon stop
end errorDialog

Last edited by peavine (2019-05-05 06:02:58 pm)


Filed under: bookmarks

Offline

 

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

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

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


iMac running OS X 10.13.1

Offline

 

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

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

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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)