Sunday, November 27, 2022

#1 2007-05-27 05:48:50 pm

Castle
Member
Registered: 2006-04-24
Posts: 38

Add an application to the Finder toolbar?

I need to automate the process of adding an application to the Finder toolbar, for one click opening. Is there any way for Applescript to add and remove Finder toolbar items? If Applescript can't do it, can it be done by Cocoa?

Thanks in advance!

Offline

 

#2 2007-05-28 12:34:54 am

Captainhams
Member
Registered: 2007-04-25
Posts: 116

Re: Add an application to the Finder toolbar?

do you mean the finder toolbar that has {Finder File Edit View Go Windw Help} ?
If so, i dont think it is possible at all, let alone automating it! hmm

Offline

 

#3 2007-05-28 04:26:17 am

Castle
Member
Registered: 2006-04-24
Posts: 38

Re: Add an application to the Finder toolbar?

I mean the toolbar (as opposed to the menubar) that has the back and forward buttons, the search box etc.

Offline

 

#4 2007-05-28 10:31:21 am

jobu
Member
From:: phxaz
Registered: 2004-01-17
Posts: 908
Website

Re: Add an application to the Finder toolbar?

The items in the Finder toolbar are finite, are determined programmatically by the Finder, and are not scriptable to the best of my knowledge.  Remember, the Finder is an application... not just 'the system'... and it has a set of guidelines that it's developers determine.  With few exceptions, I can think of few good reasons that would fit with you putting an application in the toolbar.  If you were creating some enhancement to the Finder, I might understand, but still, the Finder is not really yours to go adding features to.  And, since this is the applescript studio forum and not the plain applescript forum, I have to assume that there may be some element of commerciality to your project.  If you're going to try to market this feature to anyone but yourself and your grandmother, it's not a liberty you should be taking.  Many users would be upset if you did this to all of their Finder windows.  Besides, some people don't use the Finder toolbar... anyone can turn it off at the click of a button and then your 'easy access' is no more.

The Finder window toolbar is not your personal launcher, nor should it be.  There are plenty of places to put clickable items.  All of the following, and probably more, would be acceptable alternatives...
- the dock
- the Finder sidebar
- an endless supply of application launchers
- various menubar apps
- system services
- applescripts on your desktop
- desktop aliases

There are many ways to launch an application that fit in with apple's guidelines, and more importantly, with what users have come to expect from the mac's interface.  While even if this is just for you, it still may not be the best idea.  And in a case like this, it's just not going to be possible.  Try another approach.

j


The saddest thing I ever did see was a woodpecker peckin' at a plastic tree.
- Shel Silverstein

Offline

 

#5 2007-05-29 12:09:38 pm

Castle
Member
Registered: 2006-04-24
Posts: 38

Re: Add an application to the Finder toolbar?

Thanks for your input, Jobu. It does look like what I want would be impossible. I understand what you're saying about the Finder toolbar not being an application launcher, but the app I have in mind would be using it for a more subtle purpose, and one I personally would find extremely useful! The application wouldn't add something to your Finder toolbar without your explicitly requesting it to. No invasion! I might well be the only person who would find it useful, but I sure do big_smile

Last edited by Castle (2007-05-29 12:12:36 pm)

Offline

 

#6 2007-06-01 12:32:14 am

Dominik
Member
Registered: 2006-02-09
Posts: 493

Re: Add an application to the Finder toolbar?

jobu wrote:

The items in the Finder toolbar are finite, are determined programmatically by the Finder ...


Sorry Jobu, but I don't fully agree. It might not be well documented but you can use the Finder toolbar the same way as the sidebar. You can place your custom folders and apps there - just drag & drop them when you are in the toolbar editing mode or command-drag them if not. It should also be possible to add stuff programmatically (though I don't believe it can be done with pure AppleScript) ... ;-)

regards

Dominik

Offline

 

#7 2007-06-01 12:40:40 am

mark hunte
Member
From:: Hello, London Calling
Registered: 2006-03-20
Posts: 1030
Website

Re: Add an application to the Finder toolbar?

I had a look at this the other day when this thread started.
the com.apple.finder.plist file holds all the details.
In the FXToolbarItems

I got as far as adding into ( a copy) plist most of the info needed. But could not get the _CFURLAliasData to write.
Because it is Data with NULL characters the script (AS and do Shell) kept failing at this point. I gave up for now.

Last edited by mark hunte (2007-06-01 12:41:34 am)


Tap,tap,tap , hello , testing 1..2,  1...2,
http://www.markosx.com/thecocoaquest/

Offline

 

#8 2007-06-01 08:26:36 am

jobu
Member
From:: phxaz
Registered: 2004-01-17
Posts: 908
Website

Re: Add an application to the Finder toolbar?

Dominik, good catch.  You're right, it is possible.  This is not a feature I had seen before, and could have never imagined that the Finder had it.  I'm not exactly sure why it's possible, but I guess that apple figured (once again) that they'd disregard their own guidelines and make the Finder do something unexpected. tongue

My comments about whether you should or shouldn't do this still stand, though.  It is certainly something you shouldn't do without the users permission,.  If it's something that might be appropriate addition to the Finder's capabilities, or if you can convince your users that they really do want it in the Finder toolbar, then I retract my statement about it not belonging there and encourage you to run with it.

Thanks for the insights Dominik, and good luck to you Castle.
j


The saddest thing I ever did see was a woodpecker peckin' at a plastic tree.
- Shel Silverstein

Offline

 

#9 2007-06-01 08:58:47 am

jobu
Member
From:: phxaz
Registered: 2004-01-17
Posts: 908
Website

Re: Add an application to the Finder toolbar?

Here's the script to add an item at a given path to the Finder toolbar...

Applescript:

set thePath to "/Applications/Chess.app"
try
   set theShellScript to "defaults write com.apple.finder FXToolbarItems -array-add '<dict><key>file-data</key><dict><key>_CFURLString</key><string>" & thePath & "</string><key>_CFURLStringType</key><integer>0</integer></dict></dict>'"
   do shell script theShellScript
   do shell script "killall -HUP Finder"
end try

mark hunte, with most plist scripting I've done in apple apps, you don't need to mess with the cfurlalias data yourself.  Most of the apps will fill it in themselves when they can't find it.  All you typically need to do is kill the app and restart it using the killall command seen above.  The plist is rebuilt with the data it needs and you don't need to worry about it.  This is the same as with scripting items into the dock.

Enjoy,
j

Last edited by jobu (2007-06-01 09:04:10 am)


The saddest thing I ever did see was a woodpecker peckin' at a plastic tree.
- Shel Silverstein

Offline

 

#10 2007-06-01 10:15:48 am

mark hunte
Member
From:: Hello, London Calling
Registered: 2006-03-20
Posts: 1030
Website

Re: Add an application to the Finder toolbar?

jobu wrote:

mark hunte, with most plist scripting I've done in apple apps, you don't need to mess with the cfurlalias data yourself.  Most of the apps will fill it in themselves when they can't find it.  All you typically need to do is kill the app and restart it using the killall command seen above.  The plist is rebuilt with the data it needs and you don't need to worry about it.  This is the same as with scripting items into the dock.

Enjoy,
j


Heck, you mean I had it and did not know it roll

Thanks for that tip, jobu


Tap,tap,tap , hello , testing 1..2,  1...2,
http://www.markosx.com/thecocoaquest/

Offline

 

#11 2007-06-01 10:37:20 am

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

Re: Add an application to the Finder toolbar?

Thanks for the script, Jed. I agree that putting items in the Finder toolbar without asking is an abomination. I resent apps that scatter stuff all over the Mac GUI without asking, just as I resent web sites that change my browser window size or move it, or apps that refuse to remember where I put their window.

Having said that, I have an application (Journler) which has a drop box for adding a document in a Finder window to its journal - i.e. rather convenient importer that responds to items dragged and dropped on it. It's very handy to have it in the Finder toolbar rather than in the dock. The application didn't (and won't) put it there, I put it there (using command-drag). Having said that though, I'm going to use your script to offer a choice in one of mine. Nice.


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

Offline

 

#12 2007-06-01 11:27:03 am

mark hunte
Member
From:: Hello, London Calling
Registered: 2006-03-20
Posts: 1030
Website

Re: Add an application to the Finder toolbar?

here is the one I had, I have now taken out the _CFURLAliasData
and add Jeds  do shell script "killall -HUP Finder"

Applescript:

tell application "Finder"
   set appFolder to choose file default location (path to applications folder) as alias
   set CFURLString to "\"" & POSIX path of appFolder & "\"" as Unicode text
   
end tell
set CFURLStringType to 0
set item_id to "loc%20"

set the_bar to do shell script "defaults write com.apple.finder FXToolbarItems -array-add '<dict><key>file-data</key><dict><key>_CFURLString</key><string>" & quoted form of CFURLString & "</string><key>_CFURLStringType</key><integer>" & CFURLStringType & "</integer></dict><key>item-id</key><string>" & quoted form of item_id & "</string></dict> '"
do shell script "killall -HUP Finder"


Tap,tap,tap , hello , testing 1..2,  1...2,
http://www.markosx.com/thecocoaquest/


Filed under: Finder

Offline

 

#13 2007-06-01 02:52:04 pm

Dominik
Member
Registered: 2006-02-09
Posts: 493

Re: Add an application to the Finder toolbar?

jobu wrote:

...  with most plist scripting I've done in apple apps, you don't need to mess with the cfurlalias data yourself.  Most of the apps will fill it in themselves when they can't find it.


I should have known this ... I was curious how to solve this and spent two hours this afternoon writing a command line tool for this task - lol ;-) The only point speaking for my solution seems to be it's option to specify the index where to insert the new Item ... (or is this possible with the script solution, too?)

D.

Last edited by Dominik (2007-06-01 02:52:48 pm)

Offline

 

#14 2007-06-01 04:06:43 pm

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

Re: Add an application to the Finder toolbar?

That would be a nice addition, too, because as is, the script places it on the extreme right, just past the search box, and in my case, I would like it to be at the -2 position.


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

Offline

 

#15 2007-06-01 07:13:21 pm

mark hunte
Member
From:: Hello, London Calling
Registered: 2006-03-20
Posts: 1030
Website

Re: Add an application to the Finder toolbar?

This works, needs some cleanup but its late here... so here it is for now.
firstly my first position in the Toolbar after the normal back, forward,view, actions buttons is 4  but is counted as number 3 not sure what yours would be.
I am using PlistBuddy for part of this.

found in most cases here /Library/Receipts/AdditionalEssentials.pkg/Contents/Resources/PlistBuddy
I think some one here wrote a bit of script to always find a PlistBuddy on your mac.

The script asks for the app to be placed and the position you want it in. (The position corresponds with the entry number in the plist.  My button place is 4 but is plist entry 3,  so I choose 3.
If you have a space between to icons the space is classed as a position  and is listed in the plist as an entry.)

The script then removes the entry values for that entry and puts the new ones in.
It then uses defaults to write the original entry that was in (in my case ) position 3 to the end.

As I said it will need some clean up (in example I have not tested what happens if you enter a position that an entry does not already exist,even though I have put a bit in there for that) but this is really to show it can be done.

For me its much quicker dragging and dropping.  big_smile



Applescript:

tell application "Finder"
   set appFolder to choose file default location (path to applications folder) as alias
   set Newentry to "\"" & POSIX path of appFolder & "\"" as string
   
   
   display dialog "Enter position Number" default answer "3" buttons {"cancel", "ok"} default button 1
   copy the result as list to {place, button_pressed}
end tell
if button_pressed is "ok" then
   set readPlace to (do shell script "/Library/Receipts/AdditionalEssentials.pkg/Contents/Resources/PlistBuddy -c \"Print FXToolbarItems:" & place & "\" ~/Library/Preferences/com.apple.finder.plist") as Unicode text
   if readPlace does not contain "Does Not Exist" then
       
       set readPlace to (do shell script "/Library/Receipts/AdditionalEssentials.pkg/Contents/Resources/PlistBuddy -c \"Print FXToolbarItems:" & place & "\" ~/Library/Preferences/com.apple.finder.plist|grep -ia \"CFURLString = \"") as Unicode text
       set AppleScript's text item delimiters to "= "
       set CFURLString to text item 2 of readPlace
       
       set AppleScript's text item delimiters to ""
       set deletes to (do shell script "/Library/Receipts/AdditionalEssentials.pkg/Contents/Resources/PlistBuddy -c \"Delete FXToolbarItems:" & place & ":file-data\" ~/Library/Preferences/com.apple.finder.plist")
       
       set RePlaceCFURLString to (do shell script "/Library/Receipts/AdditionalEssentials.pkg/Contents/Resources/PlistBuddy -c \"Add FXToolbarItems:" & place & ":file-data:_CFURLString string \"" & quoted form of Newentry & "\"\" ~/Library/Preferences/com.apple.finder.plist")
       set CFURLStringType to "0"
       set RePlaceCFURLStringType to (do shell script "/Library/Receipts/AdditionalEssentials.pkg/Contents/Resources/PlistBuddy -c \"Add FXToolbarItems:" & place & ":file-data:_CFURLStringType integer \"" & CFURLStringType & "\" \" ~/Library/Preferences/com.apple.finder.plist")
       
       
       set item_id to "loc%20"
       
       set the_bar to do shell script "defaults write ~/Library/Preferences/com.apple.finder FXToolbarItems -array-add '<dict><key>file-data</key><dict><key>_CFURLString</key><string>" & quoted form of CFURLString & "</string><key>_CFURLStringType</key><integer>" & CFURLStringType & "</integer></dict><key>item-id</key><string>" & quoted form of item_id & "</string></dict> '"
       do shell script "killall -HUP Finder"
   end if
end if

Last edited by mark hunte (2007-06-01 07:15:59 pm)


Tap,tap,tap , hello , testing 1..2,  1...2,
http://www.markosx.com/thecocoaquest/


Filed under: Finder

Offline

 

#16 2007-06-02 10:19:16 am

Castle
Member
Registered: 2006-04-24
Posts: 38

Re: Add an application to the Finder toolbar?

Wow! I go away a few days and come back to find you guys have really taken this and run with it! It's looking like I'll not be able to actually find the time to make this app, and it'll be beyond my abilities to do so anyway, so I'll just tell you what I was intending. As I said above, the number of people who'll find it useful will be limited, but I know I would love it. If anyone wants to make it they should feel free to, I just ask that you make it free! Hopefully I'll be able to have another stab at it this summer, but feel free to beat me to the punch!

The app would, simply, add icons to the Finder toolbar that would allow currently selected file(s) to be open by the app represented in the icon. The icon would really just be a small applescript that tells the Finder to open the selected files with a specified app. Why do this? Well, there are a few file types that I normally open with one app, but sometimes want to open with another. I *could* right click and go to "Open With", but that's far more time consuming than just selecting the file and clicking the app's icon on the menubar. Example of when this might be useful (and the two primary occasions when I use it):

1) .001, .002, .003 etc. files can be RAR segments or segments of files split with something like Split & Concat. I normally encounter these as RAR segments, so double clicking on one will open unrarX. Every 5th time or so, though, I'll want to open the file with AJoiner.

2) .jpg files normally open with Preview, but ones saved with Photoshop open with Photoshop (at least for me) - often with no clear warning which it will be. Having the option to open any .jpg file (or other image file) with either Preview or Photoshop at the click of a button is very handy to me. No more double click lottery!

These are just two examples. My little app was going to offer users a quick, easy GUI for selecting apps to make these little scripts for and automatically add the resulting script to the Finder toolbar, with the app's original icon. User wants to add a new app, they just drag it to the GUI's window, the script is created and it's added to their toolbar. They want to remove it, then can remove it from the GUI. Ideally, it would be fashioned to look like a toolbar itself, so things could be dragged on, dragged off and re-arranged.

As I say, personally I'd find that very handy. I don't think it's invading the toolbar, as jobu suggests, since it's done at the specific request of the user. Past the adding items to the toolbar, I suspect that the icon manipulation would be impossible with Applescript and the GUI would be able to be far more intuitive using techniques that would require Cocoa.

If you're interested in developing this small app, let me know and I'll outline what I envisioned in more detail! Anyone is welcome to take it and make it for free as long as they distribute it for free - if you wanted to make a shareware app out of it, though, I'd expect a lot more involvement in the design and profiting tongue

Thanks for everyone's input, though!

Offline

 

#17 2007-06-02 01:12:05 pm

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

Re: Add an application to the Finder toolbar?

mark hunte wrote:

This works, needs some cleanup but its late here... so here it is for now.
firstly my first position in the Toolbar after the normal back, forward,view, actions buttons is 4  but is counted as number 3 not sure what yours would be.
I am using PlistBuddy for part of this.

found in most cases here /Library/Receipts/AdditionalEssentials.pkg/Contents/Resources/PlistBuddy
I think some one here wrote a bit of script to always find a PlistBuddy on your mac.


WaltR included these lines in a script for altering the dock contents:

Applescript:

try
   set myPlistBuddy to (do shell script "/usr/bin/locate PlistBuddy | sed 2,10000d")
end try

Mine is in the same place as yours. Agree with command-dragging for most cases.


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

Offline

 

#18 2007-06-09 05:45:38 am

mark hunte
Member
From:: Hello, London Calling
Registered: 2006-03-20
Posts: 1030
Website

Re: Add an application to the Finder toolbar?

hi Castle,
Here is a starter for your app
I am sure it can be improved, as I just threw it to gather out of bits of my scripts, as time today is an issue.. smile


Applescript:


property AppTarg1 : ""
property AppTarg2 : ""
property file_spec : ""
tell application "Finder"
   set S to (get selection)
   set AppTarg1 to displayed name of (choose file default location (path to applications folder) without invisibles and multiple selections allowed)
   
   set AppTarg2 to displayed name of (choose file default location (path to applications folder) without invisibles and multiple selections allowed)
   display dialog "Set Name of New Open App" default answer "openWith" buttons {"OK"} default button 1
   copy the result as list to {Script_app, button_pressed}
   set biglist to "property biglist : {" & quote & AppTarg1 & quote & "," & quote & AppTarg2 & quote & "}"
end tell

set script_text to MakeScript(biglist)
my RunMaker(script_text, Script_app, biglist)
my insert(file_spec)
on RunMaker(script_text, Script_app, biglist)
   
   tell application "Script Editor"
       set file_spec to (path to applications folder as Unicode text) & (Script_app & ".app" as string)
       
       make document with properties {text:script_text}
       save document 1 as "application bundle" in file file_spec without stay open and startup screen
       close window 1
       return file_spec
   end tell
end RunMaker

on MakeScript(biglist)
   
   set Thescript to biglist & return & "
       
       tell application \"Finder\"
       set S to (get selection)
           set K to choose from list biglist OK button name \"Open Selected Apps\" with prompt \"Select application you'd like to Open the Selected files with\" with title \"Open With..\" without multiple selections allowed
           
       end tell
   
       if K is not false then -- not cancel button
           set K to K as string
repeat with i from 1 to number of items in S
       set this_item to item i of S as alias
       tell application K to open this_item
   end repeat
   
end if

   "

   return Thescript as Unicode text
end MakeScript

on insert(file_spec)
   tell application "Finder"
       set appFile to file_spec
       set Newentry to "\"" & POSIX path of appFile & "\"" as string
       
       
       display dialog "Enter position Number" default answer "3" buttons {"cancel", "ok"} default button 2
       copy the result as list to {place, button_pressed}
   end tell
   
   if button_pressed is "Ok" then
       try
           set myPlistBuddy to (do shell script "/usr/bin/locate PlistBuddy | sed 2,10000d")
       end try
       set readPlace to (do shell script myPlistBuddy & " -c \"Print FXToolbarItems:" & place & "\" ~/Library/Preferences/com.apple.finder.plist") as Unicode text
       if readPlace does not contain "Does Not Exist" then
           
           set readPlace to (do shell script myPlistBuddy & " -c \"Print FXToolbarItems:" & place & "\" ~/Library/Preferences/com.apple.finder.plist|grep -ia \"CFURLString = \"") as Unicode text
           set AppleScript's text item delimiters to "= "
           set CFURLString to text item 2 of readPlace
           
           set AppleScript's text item delimiters to ""
           set deletes to (do shell script myPlistBuddy & " -c \"Delete FXToolbarItems:" & place & ":file-data\" ~/Library/Preferences/com.apple.finder.plist")
           
           set RePlaceCFURLString to (do shell script myPlistBuddy & " -c \"Add FXToolbarItems:" & place & ":file-data:_CFURLString string \"" & quoted form of Newentry & "\"\" ~/Library/Preferences/com.apple.finder.plist")
           set CFURLStringType to "0"
           set RePlaceCFURLStringType to (do shell script myPlistBuddy & " -c \"Add FXToolbarItems:" & place & ":file-data:_CFURLStringType integer \"" & CFURLStringType & "\" \" ~/Library/Preferences/com.apple.finder.plist")
           
           
           set item_id to "loc%20"
           
           set the_bar to do shell script "defaults write ~/Library/Preferences/com.apple.finder FXToolbarItems -array-add '<dict><key>file-data</key><dict><key>_CFURLString</key><string>" & quoted form of CFURLString & "</string><key>_CFURLStringType</key><integer>" & CFURLStringType & "</integer></dict><key>item-id</key><string>" & quoted form of item_id & "</string></dict> '"
           do shell script "killall -HUP Finder"
       end if
   end if
end insert

*edit* opps forgot to add waltr's  myPlistBuddy , (Thanks Adam)

Last edited by mark hunte (2007-06-09 05:54:34 am)


Tap,tap,tap , hello , testing 1..2,  1...2,
http://www.markosx.com/thecocoaquest/


Filed under: Script, Finder

Offline

 

#19 2007-06-10 04:46:14 pm

Castle
Member
Registered: 2006-04-24
Posts: 38

Re: Add an application to the Finder toolbar?

Thanks mark! I'll try to take a good look at it soon! big_smile

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)