simple login script doing my head in

Right. This shouldn’t be as much trouble as it is.

All I want is just a simple script which will run on log-in to open some apps, hide them, then open some bookmarks in safari. The bookmarks are all under one button in the bookmarks bar, which you only have to click once because I’ve enabled auto-click. So the GUI scripting does that. At least it should.

Two problems:

For some reason the script is asking for permission to run each time… ‘Press Run to run this script.’ I just want it to run. Can anyone tell me why its doing this?

Secondly, it’s appears to be entirely random whether or not it works. Many times I get an error something like NSUreciever error (I can’t remember exactly because it’s random and I can’t always repeat it!). Is this something to do with Safari not having an open window for the GUI scripting to go to work on? I’ve added various delays to try to ensure the other apps have all been hidden and safari has had time to launch properly and appear. But it still seems random.

Whats wrong? Is there a better way to do this?


property appsToStart : {"Adium", "iTunes", "BluePhoneElite", "WeatherDock", "Mail", "Acquisition", "TuneX"}

repeat with appToStart in appsToStart
	
	with timeout of 120 seconds
		tell application appToStart to launch
		delay 2
	end timeout
	
	delay 2
	
	tell application "System Events"
		try
			set visible of process appToStart to false --to hide the open apps
		end try
	end tell
	
end repeat
delay 5 --to give time for Mail to finish being 'hid' (I noticed when Mail was still visible, sometimes Safari had trouble appearing)
tell application "Safari" to launch
delay 5 --to give Safari window time to appear (am I right in thinking without a visible window, the GUI scripting will just fail out right?)
tell application "System Events" to tell process "Safari"
	repeat with aGroup in groups of window 1
		if (button "Good Morning" of aGroup exists) then
			click button "Good Morning" of aGroup
		end if
	end repeat
end tell

Hi splodgecat,

When you use something like:

repeat with appToStart in appsToStart

appToStart is a reference to a list item. Some app in your script may not be able to deal with this in the ‘tell’ block or what ever, so try getting its value with ‘contents’. Also, you don’t need ui scripting to open a page, but may just use its url. Something like this:


property appsToStart : {"iTunes", "Stickies"} -- { "Adium" ,"iTunes" ,"BluePhoneElite" ,"WeatherDock" ,"Mail" ,"Acquisition" ,"TuneX" }
--
repeat with appToStart in appsToStart
	tell application appToStart to launch
end repeat
repeat with appToStart in appsToStart
	set this_process to (contents of appToStart)
	tell application "System Events"
		repeat until exists process this_process
		end repeat
		set visible of process this_process to false
	end tell
end repeat
tell application "Safari"
	activate
	open location "http://macscripter.net/"
end tell

Here, I only used “iTuens” and “Stickies”, so change that. Also, iTunes takes a long time to launch, so that might be the cause of your error when you set visible of a process to true. In other words, the process may not exist yet (even with the delays).

gl,

Thanks kel. That’s awesome, I’ll make the adjustments in my script.

But I think the errors actually come from the Safari bit of the script. All my apps open flawlessly, and then Safari pops open, and BAM, error. And the error always appears when the safari window isnt visible yet.

And I’d like to keep the use of the auto-click bookmark button because it allows easy editing of the urls that i want to see, and also opens them all neatly, simultaneously, in tabs.

Perhaps if i use GUI scripting to go through the menu bar would that be better than using the bookmarks bar in the main window?

Also, why do i have to give the script confirmation to run? When im editing it in script editor, i can click run and it goes right ahead and does it all by itself. but when it starts up on login (having added to login items via accounts) it displays a dialogue box saying ‘press run to run this script’. why is this? i just want it to run.

cheers

When you compile and save the script, you need to check don’t show startup window or something like that. Then you won’t get that window asking if you want to run the script.

Personally, I would stay away from hitting buttons in the bookmarks bar. I’d do something like drop a url or choose from a list the default starting url. You never know when references to ui elements might change.

gl,

Thanks for your help mate. I’ve settled on a script that brings the Safari window to the front before it uses the GUI scripting, which works brilliantly. And I’ve unticked Start-up screen when saving the script, thank you! a simple mistake. Cheers!