Keep app on Dock - hide and show the Dock?

I have an app that when run does some do shell scripts and then quits. One of the scripts it does it to kill the Dock. If the app is added to the Dock and then run it sometimes stays put, but sometimes get lost from the Dock. I’ve worked out that making a change to the Dock - such as hiding it and showing it again - will force the Dock to remember that the app has been put on it.

However, I’m having trouble getting the app to hide and show the dock again.

I tried:


tell application "System Events"
					keystroke "d" using {command down, option down}
					keystroke "d" using {command down, option down}
				end tell

This works sometimes, but sometimes does not - only one keystroke gets registered. I assume this is because the app is pushing ahead too quick and getting to the point where it quits before registering the second keystroke. Sometimes the Dock stays hidden after the app has run. If I use a button to trigger the tell to System Events it works perfectly, but when it is triggered automatically by the launch of the app it is far more temperamental.

So how can I get it to hide the Dock, reshow it and then do all the other stuff the app does automatically?

Or is there a better way entirely?

Many thanks for your help!

Hi,

if you wrote this app yourself, I would recommend to improve the code to avoid the strange behavior,
otherwise a little delay could help

tell application "System Events"
	keystroke "d" using {command down, option down}
	delay 0.5
	keystroke "d" using {command down, option down}
end tell

or with Leopard, the Dock preferences are scriptable


tell application "System Events"
	tell dock preferences
		set autohide to true
		delay 0.5
		set autohide to false
	end tell
end tell

The weird behaviour happens regardless of it the code is run in the main app or in a separate test app. I’m not sure how the code could be improved, but I’m sure it could be. I’ll try scripting the prefs directly, but I’m not sure if that will work since the problem seems in part to be because of the Dock not recognising changes quickly enough before it is restarted. Thanks for the advice, though, I’ll give it a try!

Update: Neither of the suggested methods work. They both result still in only the first keypress or change to the options being registered and remembered.

Is there no way to force Applescript to run a particular piece of code before moving on the rest of the code?

Hiding and showing does not restart the dock, if you want to do this explicitly, use

do shell script "killall Dock"

I’m wanting to hide and show the Dock because that forces the Dock to recheck its preferences. I’m doing this before “killall Dock”. I’m finding that if I “killall Dock” the last app added to the Dock doesn’t always get remembered. If I add the app, then press hide and show the Dock it always gets remembered. I’m trying to force my app to quickly hide and show the Dock before doing anything else to ensure the app gets remembered if it has just been added to the Dock. Currently when it kills the Dock the Dock reverts to before the app was added.

To see what I mean do the following:

Open up Terminal.
Drag an application to the Dock.
Run “killall Dock” using the Terminal window
The Dock will restart
The app icon you added will (probably) be gone.

Now try adding the app, hiding and showing the Dock, and then killing it. When the Dock restarts the icon will still be there.

Eventually got around it with this:


do shell script "chflags uchg " & preferencesPath
tell application "Dock" to quit
do shell script "chflags nouchg " & preferencesPath

Where preferencesPath is the path to the Dock’s preference file (~/Library/Preferences/com.apple.dock.plist, normally)

This seemed to to the job.