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.
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.
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
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
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
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?
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