Removing Items from Sidebar

Hi there,

I’m trying to write a script that will allow me to manage the Sidebar list.

It’s still at a very early stage, I’m still playing with deleting or removing an item from the list. I’ve hit a slight snag and I’m not sure how to solve it. How do I tell the Finder to remove an item from the list?
I’ve had a look through the Finder dictionary and found the ‘delete’ command but I’m not sure if I can use it in the way I am. I’m coming up with an ‘NSReceiverEvaluationScriptError: 4’ which, correct me if I’m wrong, makes me think the script can’t find the item I’m trying to get rid of.

Here’s the code:-

tell application "System Events"
	tell process "finder"
		delete UI element "ELEMENT" of list 1 of scroll area 1 of splitter group 1 of window "THIS_WINDOW"
	end tell
end tell

Any help would be appreciated.

Thanks in advance,

Nick

If you are trying to do this in a networked enviroment and have access to OS X Tiger Sever, there’s an easier way.

Hi Ben,

Thanks for the help.

Yeah I’d seen something on a forum regarding this, however that’s not the environment I’m working in so I think it wouldn’t be an option.

Thanks again,

Nick

If I control-click on an item in the sidebar a submenu appears.
There’s a ‘Remove From Sidebar’ menu item, does anyone know if I can access that with AS?

Thanks in advance,

Nick

Sidebar descriptions are kept in ~/Library/Preferences/com.apple.sidebars.plist. Maybe you could go there. The top pane is in “systemitems” and the user pane is in “useritems”

[EDIT] - you can add a selection in a Finder window to the user’s sidebar with Command-T

Hi Adam,

Thanks for the help, that was one of the solutions I’d already found whilst searching for an answer.
I was hoping to have a little more control over the process e.g. being able select multiple items from a list and have those removed.

Having tried one or two things this is where I’m up to:-

tell application "System Events"
	tell process "Finder"
		key down control
		delay 0.5
		select UI element "ELEMENT" of list 1 of scroll area 1 of splitter group 1 of window "THIS_WINDOW"
		delay 0.5
		key up control
	end tell
end tell

I’m still having problems in that both the ‘key down control’ and the ‘Select UI Element’ work separately but not together?
I’ve been trying to ‘control-click’ on an item, in the sidebar, to bring up the submenu. From there I can select ‘Remove From Sidebar’.

Any further thoughts would be appreciated.

Thanks in advance,

Nick

What about removing the sidebar.plist then adding what you want via an Applescript?


tell application "System Events"
	
	open desktop folder
	delay 0.25
	keystroke "t" using command down
	delay 0.25
end tell

Thanks for the help Ben.
Yeah I guess that’s one way to go. When I was having a search around for something I’d found some stuff on a few forums which suggested the same. One thing that was mentioned was that after doing this you’d need to reboot for the changes to take effect. That’s one of the reasons I’d not played with this option. I’m not sure if the reboot bit is correct?

I’d been trying to get the other option working because it feels like I’m really close to a solution.

Thanks again for your help, it’s much appreciated.

Nick

You’d need to logout & back in after removing the sidebar.plist for that to take effect.

But adding items will happen in “real-time”

It might be sufficient to quit and restart the Finder.

Thanks for the help guys.

Think I might have to go for plan b!

I thought I was pretty close with the script.

Regards,

Nick

Hi.

It turns out that when GUI scripted, the side panel items each have an “AXShowMenu” action.

on chooseItems from theList under prmpt
	tell application (path to frontmost application as Unicode text)
		return (choose from list theList with prompt prmpt with multiple selections allowed)
	end tell
end chooseItems

set computerName to computer name of (system info)
set desktopName to displayed name of (info for (path to desktop))
set networkName to displayed name of (info for alias "Network:")
set userName to displayed name of (info for (path to current user folder))

tell application "Finder"
	if ((count each Finder window) is 0) then make new Finder window
end tell

tell application "System Events"
	tell application process "Finder"
		set frontmost to true
		tell list 1 of scroll area 1 of splitter group 1 of window 1
			set sidePanelNames to name of UI elements
			set itemsToDelete to my (chooseItems from sidePanelNames under "Choose which side panel items to delete:")
			if (itemsToDelete is false) then error number -128
			
			repeat with i from 1 to (count itemsToDelete)
				set thisItem to item i of itemsToDelete
				perform action "AXShowMenu" of UI element thisItem
				if (thisItem is computerName) then
					key code {125, 125, 125, 125, 49}
				else if (thisItem is networkName) or (thisItem is userName) then
					key code {125, 125, 125, 125, 125, 49}
				else if (thisItem is desktopName) then
					key code {125, 125, 125, 125, 125, 125, 125, 49}
				else
					key code {125, 125, 125, 125, 125, 125, 49}
				end if
				delay 0.2 -- Lengthen if necessary.
			end repeat
		end tell
	end tell
end tell

Edit: I think this will now work with most user languages for most items. But I don’t have an iDisk with which to test that particular possiblity. It may also need a ‘try’ block round the ‘set networkName’ line, setting the variable to “” in the ‘on error’ section if there’s no Network alias.

I’ve used ‘displayed names’ in the script because “ on my machine, at least “ the ‘name’ of the Desktop alias is always “Desktop” and that of the Network one always “Network”, whereas their ‘displayed names’ are the equivalents in the local language. (eg. In French: “Bureau” and “Réseau”.)

The script may not work properly if the side panel contains a folder belonging to another user “ especially if that folder’s chosen as the target!

Hi Nigel,

Thank you for your help with this query and for coding a solution.
It has shown me one or two other things besides the removal of items from the sidebar.

I’d seen the “AXShowMenu” action when using the UI Element Inspector to try and find a solution.
I was at the stage of trying to find a way of using the action when I found your solution.
This is where I was at:-

tell application "System Events"
	tell process "Finder"
		perform action "AXShowMenu" of UI element "THIS_ELEMENT" of list 1 of scroll area 1 of splitter group 1 of window "THIS_WINDOW" of application process "Finder" of application "System Events"
	end tell
end tell

Upon getting this to work I was going to take it forward with a loop and keystrokes.

One thing I’d not considered was checking what type of item I might be removing and how this would affect the
keycodes.

On trying your script I did find I had to place a try block around the ‘set networkName’, I did get a problem when running
the script on a laptop that was not on a network. It did work on a networked machine. Thanks for the suggestion.

Another thing I did do was to put a ‘select UI element thisItem’ line in the loop that does the deleting. That seemed to help, previously I
did get a few ‘NSReceiverEvaluationScriptError:4’ errors.

Once again many thanks for your time on this one, it’ll make the management of the sidebars alot easier.

Regards,

Nick :slight_smile: