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!

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! :confused:

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

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

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

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) … :wink:

regards

Dominik

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.

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. :stuck_out_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

Here’s the script to add an item at a given path to the Finder toolbar…

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

Heck, you mean I had it and did not know it :rolleyes:

Thanks for that tip, jobu

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.

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

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"

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 :wink: 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.

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.

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. :smiley:

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

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

Thanks for everyone’s input, though!

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

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.

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… :slight_smile:


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)

Thanks mark! I’ll try to take a good look at it soon! :smiley: