Writing to/accesing .plist to save hide, show dock icon

I have an app and I want to add the feature that allows the user to hide and show the dock icon. I know that you can make it hide all the time by adding

LSUIElement
1

to the .plist in the XCode file.

So, to include this feature I simple want the app to read that file and add or take out those 2 lines based on whether or not the user wants them. However, I have two issues.

  1. I don’t know how to access that file, but I’m guessing its something like
    Example.app/contents/example.plist

  2. I’m not sure is applscript can write to .plist files now, but i know it will be able to in Leopard, so will i have to wait to intergrate my code?

Thanks,
Josh

The LSUIElement parameter is always in Contents/Info.plist of an app.
Of course AppleScript (actually the shell) can write into plist files


do shell script "defaults write /Applications/Example.app/Contents/Info LSUIElement 1"

PS: There are very helpful tutorials on ScriptWire :wink:

Thanks, your script works for getting rid of the icon perfectly, but how would i make it so it can be put back in the dock?

I tried:

do shell script "defaults DELETE /Applications/PodNod.app/Contents/Info LSUIElement 1"

but got an error. I also tried erase.

try delete (all lowercase)

haha, thats what i did, i just capatilized it in my post to put emphasis on what i changed,

maybe since its a shell script, i should use rm?

.and without the parameter at the end

do shell script "defaults delete /Applications/PodNod.app/Contents/Info LSUIElement"

PS: rm deletes the whole plist file

I tried that, and I’m getting a “2007-10-25 11:41:59.487 defaults[875]
There is no (LSUIElement) default for the (/Applications/Example.app/Contents/Info) domain.
Defaults have not been changed.”

Also, now for some reason the writing one isn’t working, either. I didn’t delete the .plist, i checked.

try this: it toggles the state of LSUIelement (1 or not existing)


property theApp : "PodNod"

set i to (path to startup disk as Unicode text) & "Applications:" & theApp & ".app:"
try
	(i & "Contents:Info.plist") as alias -- check if Info.plist exists
	set infoPlist to quoted form of POSIX path of (i & "Contents:Info")
	try
		do shell script "defaults read " & infoPlist & " LSUIElement"
		do shell script "defaults delete " & infoPlist & " LSUIElement"
		display dialog "Dock Icon of application " & theApp & " will be showed"
	on error
		do shell script "defaults write " & infoPlist & " LSUIElement 1"
		display dialog "Dock Icon of of application " & theApp & " will be hidden" buttons {"OK"} default button 1
		do shell script "touch " & quoted form of POSIX path of i
	end try
on error
	display dialog "file Info.plist of application PodNod doesn't exist"
end try

Hey, that works almost perfectly, thanks! It weird though in that the app has to be running when switching the icon on, but when switching the icon off, it doesnt have to be running.

Thanks for the script, though!