STOP adding multiple login items!

Hi,

The current program I am making has a bug. In this program, you click OK, and it adds itself to the login items. I know how to do this already.

Say a person opens the window again, and clicks OK again. Then you get 2 entries in the login items list! I’m wondering how to prevent this from happening.

maybe like so?

tell application "System Events"
	if not ((name of login items) contains "nameofyourapp") then
		-- add login item
	else
		-- item already exists
	end if
end

Great! :smiley:

You just made one mistake. I corrected it. :slight_smile: :

 if ((name of login items) does not contain "nameofyourapp") then

Looks like the same code to me, just worded differently.

Worded differently=don’t work :stuck_out_tongue:

if not ((name of login items) contains "nameofyourapp") then

gives an error, while mines doesn’t :slight_smile:

And aren’t you from OSX Hints?

Really, it’s the same and shouldn’t error. That’s what I get for assuming AppleScript doesn’t have its head up its butt, I guess.

Yes, I post on Mac OS X Hints. :wink:

They both work for me ThreeDee.

tell application "System Events"
	if ((name of login items) does not contain "Not there") then beep
end tell

tell application "System Events"
	if not ((name of login items) contains "Not there") then beep 2
end tell

Well, not for me in Xcode… idk. It doesn’t really matter.

Hmm. neither work for me in an AS Studio app.

Here’s the actual error message:

Both work for me on most computers I have tried, but in my office, there are two computers which get an error from it. All the computers are running Tiger with all the latest updates to 10.4.10 and are all (except mine) Intel based. But on just two of them I get this error:

"Applescript Error

System Events got an error: Can’t make name of every login item into type reference. (-1700)"

Please if anyone could shed any light on this I would be extremely grateful, as I check for the existence of the login item when the application is started, so this modal error has to be dismissed before the user can start working with the app and is therefore irritating the two users that it effects. I’m frustrated that even though they appear to have identical installs of OS X and the same version of System Events, the error is occuring on those two Intel iMacs and not on any other Intel/PowerPC iMac.

Help!

-Ryan

Hey

Thanks for the suggestion. I tried your code, and it works on the computers where the other code worked, but there is still a problem with the two computers that didn’t work before.

There is no error message given or anything, but the code still fails to detect if there is a login item present and therefore still stops me doing what I’m trying to do :frowning:

Do you have any other suggestions at all?

Thanks for your help
Ryan

Hi Ryan,

here an idea for a workaround (should result yes if the item exists and no, if not …)

get my testForLoginItem("iTunesHelper") -- for example ...

on testForLoginItem(loginItemName)
	try
		get (do shell script "cat ~/Library/Preferences/loginwindow.plist | grep --binary-files=text '" & loginItemName & ".app'")
		return yes
	end try
	return no
end testForLoginItem

hope that helps …

D.

Excellent stuff, that workaround does indeed work for all the clients.

My problem is now that the same problem that was effecting the two clients before, is still effecting them when I try to delete the login item.

I can add a new login item fine, but the deletion code gives a similar error as before on those same two computers.

Here is the code I’m using:



		tell application "System Events"
			
			delete (first login item whose name contains "MyAppsName.app")
			
		end tell


Do you have any idea what that could be? I’m thinking there is a big problem with System Events on certain machines here :frowning:

Ryan,

have you tried to find out what part of the script fails?

does this work?

tell application "System Events"
	get login items
end tell

if this works, then try this:

tell application "System Events"
	set li to login items
	set reslt to {}
	repeat with this_li in li
		try
			copy (get name of this_li) to the end of reslt
		on error
			copy "error" to the end of reslt
		end try
	end repeat
	get reslt
end tell

If there is a problem with the name of a single login item, a script like this might bring a result.

If the identifying via name fails maybe it is a solution to identify the app by the property ‘path’?

try if this works:

tell application "System Events"
	get path of login items
end tell

D.

Yes this ran without an error on all the machines.

This code works, as in I can check if my app is located in the ‘reslt’ variable, but I get an error:

No result was returned from some part of this expression. (-2763)

Thanks for the tips, I’ll keep looking at the code to see if I can spot anything.

Having tried a slightly modified version of this:


	tell application "System Events"
		set ryan to (path of login items as string)
	end tell

I get the following error on the same two machines as before, but it works on every other machine:

AppleScript Error
Can’t make «class ppth» of every «class logi» of application “System Events” into type string. (-1700).

I also get the same error if I try Unicode text instead of string. It’s driving me mad!

My idea here was, that you try to find out if it is a specific login item that leads to this error. (I hoped the script would result a list like this {app1, app2, app3, error, app5, app6} - the missing login item - replaced by ‘error’ in the list - would be the problem then …)

Another idea …
Have you tried removing all startup items and adding them again on one of these machines? Maybe the .plist is corrupted and this will fix the problem? Does the plist open in Property List Editor?

D.

Ok so when i run your code I have no made it log the variable ‘reslt’ and then display a dialog containing item 1 of reslt.

On the ‘normal’ computers I get this in the log:

{“iTunesHelper”, “MenuCalendarClock iCal”}

And ‘iTunesHelper’ in the dialog box, which is great.

On the two computers where the world has gone mad, I get nothing, no error and no output.

I have tried trashing the .plist file and starting again to no avail. There is obviously something totally crazy going on in this system!

another idea …

to make sure, that the problem is not on the applescript side, you could download, compile and execute an Apple samplecode project:

http://developer.apple.com/samplecode/LoginItemsAE/index.html

Good idea, I have tried this example app on the ‘normal’ computers and it works as expected, however on the two affected machines, it gives an error and then shows an empty table regardless of whether there are any login items.

So I think this pretty much proves that this is a problem with AppleScript/System Events on those two machines.

I’ve managed to find a way around it though - using some example code to add/remove login items in Objective-C, I have written two methods which I can then run from AppleScript via ‘call method’ - and it works for all the computers!

Thanks for all your help in trying to get it to work in AppleScript :slight_smile: