Detect / insert data into .plist with applescript?

The short story is that I hope to add an user specific Active Directory managed folder (My Documents) to the Finder sidebar the first time an individual logs into Mac OS 10.3 or 10.4.

With your help I have put together a script that will locate and find an individual’s Windows folder and open it in a Finder window. This script runs at login for all users.

The next step is that I would like for the script to add an alias to this location to the sidebar of the Finder. This seems to involve editing the (binary or Hex?) in the com.apple.sidebarlists.plist. Is this possible with applescript?

After that I would like to have the script check the .plist to see if this value has been added and if so then cancel the script so it won’t run over and over for every user at each login.

I’m essentially looking for some advice here. I haven’t found any applescript info about adding an alias to the Finder sidebar.
is it possible? Is there an easier way than editing the .plist file directly?

Thanks,
KJ

Attacking the code in com.apple.sidebars.plist seems pretty ugly. I haven’t try this, but have you created a new user, grabbed the unadorned .plist, then added the item you want there, and grabbed the .plist again to see what changed?

Would it be acceptable for you to use something like this?

tell application "Finder"
	activate
	reveal (choose folder)
end tell

tell application "System Events"
	launch
	keystroke "t" using command down
end tell

tell application "Finder" to close front Finder window

Just replace “choose folder” with your information.

Hi KJ,

there is a shell command (can be implemented with ‘do shell script’ in AS) for manipulating plists:

defaults read com.apple.sidebarlists useritems
reads the object for the key “useritems” of the sidebar (the part you can edit)

and
defaults write com.apple.sidebarlists useritems value
writes it.

I am afraid the main problem is to find an AppleScript way to setup your new entry object which looks like so:

    {
        Alias = <00000000 00f80002 00010141 00000000 00000000 00000000 00000000 00000000 00000000 0000bd79 a4fe482b 00000000 00020756 6f6c756d 65730000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000003 4a9dbd79 b1310000 00000000 0000ffff ffff0000 09200000 00000000 00000000 00000000 00014100 00100008 0000bd79 88de0000 00110008 0000bd79 95110000 00010000 00020009 413a566f 6c756d65 7300000e 00100007 0056006f 006c0075 006d0065 0073000f 00040001 00410012 0007566f 6c756d65 73000013 00012f00 ffff0000 >; 
        Icon = <496d6752 0000001c 00000000 5359534c 00000010 00000000 666c6472 >; 
        Name = Volumes; 
    },

If you happen to find a solution, please post it here :wink:

Btw. I agree with Adam, you really better should try this with a newly created user first …

Dominik

Agreed. I thought there would be a similar way to reproduce a “drag and drop” process with applescript. I can’t find one. Is this what you mean by GUI scripting?

Yes. It looks like a mess. See Dominik’s example.

kj

My script uses System Events to press command+T, which is the default keyboard shortcut for the “Add to Sidebar” menu item.

mistakenly referred to this as GUI scripting; Key pressing does not require GUI scripting to be turned on. I have removed that term in my original post.

Mr Phillips, thanks for all your help.
Scripting the command+T was just the sort of thing that I knew must be available in the OS and applescript.
I’ve only had Macs in my classroom for a couple of weeks and don’t know my way around them yet.
When I share this with other teachers using Mac in our Windows school district you should be a certified hero.

So it works great, and I realize after re-writing using your suggestions that every line belongs to you.

tell application "Finder"
	activate
	
	display dialog "Please enter the year you graduate:" default answer "" with icon 1
	set UserInput to the text returned of result
	
	display dialog "Please enter your username:" default answer "" with icon 1
	set UserInput2 to the text returned of result
	
	reveal ("STUDENTHOME:" & UserInput & ":" & UserInput2 & ":")
end tell

tell application "System Events"
	launch
	keystroke "t" using command down
end tell

tell application "Finder" to close front Finder window

At the risk of revealing myself as compleatly helpless - any tips on how to check and see if the folder is already present in the sidebar and if so cancel the script from running? I see this as a basic if / then situation but the only thing I can think of checking is to verify the presence of the sidebar alias is the .plist file. Is there an easier way?

thanks again.
kj

If the folder is already in the sidebar, then that menu item will be disabled. Thus, the script will still press command+T, but nothing will happen.