Relaunch as hidden from dock problem


I’m trying to get my app to relaunch in a state that it is not showing the icon in the dock.

I figured with the following script it should work but when relaunched, the icon is still showing and NSUIElement is still set to 0. But when I check NSUIElement just before the app closes it is set to 1. What am I missing here?

on SetDock(V)
	tell application "System Events" to set infoPlist to ((application file of (first application process whose name contains "TestApp") as string) & "Contents:Info.plist")
		do shell script "defaults write " & infoPlist & " NSUIElement -int " & V
	end try
end SetDock

on ReLaunch()
	set shScript to "sleep 2; open " & quoted form of POSIX path of (path to me)
	do shell script "echo " & quoted form of shScript & " > /tmp/l" --> store in temp file
	do shell script "chmod 755 /tmp/l; /tmp/l > /dev/null 2>&1 &" --> execute it "ignoring responses", and quit
	do shell script "killall -c TestApp"
end ReLaunch

Do not modify applications internally. There are too many user concerns and technical complications involved. (You really should never need to do this.)

What is the purpose behind relaunching hidden? Perhaps there is another angle from which to approach your task.

You’re right, fooling around with bundle content is not a great idea. I wanted to be able to create a button that would send the app in ‘stealth mode’. I now used a different approach with NSUIElement at 1 but with the disadvantage that my menu is not appearing anymore. The most important thing tho is that the dock icon is absent.

I’m still not grasping the purpose behind doing what you’re trying to do. Without knowing anything about this application at all–since you haven’t offered up any information–there’s no way to recommend any course of action here. (Other than “modifying the contents of the application bundle is not good design”, that is.)

As an FYI:

Background-only apps cannot gain full focus, therefore they cannot display their menu bar.

Based on this discussion I created an AppleScript Studio project that would monitor possible theft. To ‘cloak’ the program I wanted to enable a ‘stealth mode’ that would run the program in the background (no dock icon, etc) and scan for changes in WAN IP and SSID as well detecting motion. I thought I could create a button that would relaunch the app in this stealth mode, instead I opted for a different approach where I start the app in background mode (NSUIElement at 1). This seems to work fine except for the fact that it doesn’t focus the windows well.

As the code is too long, I won’t post it but if anybody is interested I can send the project files. Just send me your email address by pm.

No need for the project files. Your conceptual approach is simply wrong. (No offense intended.)

What you require is a second background process that you can control with a GUI through some manner. (Standard app, system preference pane, menu bar item, etc.) This would be not a modification of your single app, but a second target in your project that builds a second application with no GUI at all.

No offense taken, I appreciate constructive feedback!

I actually started along the path you suggested but ran into too many problems with it. This is most definitely due to my inexperience with AppleScript. I may in the next few days see if I can restart that path and if I can overcome the problems I had initially (can’t even remember them, this has been an ongoing learn as I go project). With the few AS books I bought just recently, I may just crack it.

Reviving and ancient thread here. (Though it seems to be the wrong forum for it.)

I’ve been poking around on this. I have an AS Studio app that’s always been developed as a background app with NSUIElement = 1 in the info.plist. I’ve changed that now to = 0 (normal app).

I’d like to be able to switch it back and forth and tried the write-to Info.plist approach from an external script. (Despite knowing that’s not advised.) Won’t go into details but I now see why :wink:

Anyway, it seems this is not too hard to pull off in C. You must start it background but you can programmatically make it a dock app while running.

but how could I do it in AS Studio with a call method?

Also, is the unix id property of a process the same as the PSN? So would this be a viable start?

start app with NSUIElement = 1 in Info.plist; background
on will finish launching
-- read user defaults
if not run_background then
tell application "System Events"
	set tproc to item 1 of (every process whose name contains "MyAppName")
	set psn to unix id of tproc
end tell
set OSStatus_return to call method TransformProcessType with parameter (psn) 
end if
end will finish launching


I managed to get this to work with a custom class, sort of. so don’t sweat any replies too much now. but still curious to know:

a) about the unix id = psn?
b) whether TransformProcessType can be called directly without creating a custom class
c) if there’s any other easy way to do this that I’m missing

one problem with my implementation:

if my app is saved in a position in the dock, when launched “transformed” into a normal app, the “active” glowing arrow icon (what’s that called?) in the dock is not shown. conversely, when I take it out of the dock, then when launch it from the finder it shows as a normal application in the dock would, but at the end of the list. that’s a little irritating…