New to Scripting - How to modify a script?

Hello!

Being new to Applescript, I am inquiring on how to modify a script (pasted below). The script will allow iWeb to automtically publish every fifteen minutes (so I can update a blog while on the road via iPhone). When the script first opens, a window requires my making a selection, “Stop” or “Begin”. Because I want to “Begin”, is there a way to modify the script to automatically begin the program without interacting with this window first?

Thanks,

Doug Ramsey :slight_smile:


SCRIPT

on run
	set the icon_file to path to resource "applet.icns"
	display dialog "This script will attempt to publish your iWeb website every 15 minutes." buttons {"Stop", "Begin"} default button 1 with title "Auto-Publsih iWeb" with icon icon_file
	if button returned of the result is "Stop" then
		tell me to quit
	end if
end run

on idle
	try
		tell application "iWeb" to activate
		tell application "System Events"
			-- enable UI scripting if it is off
			if UI elements enabled is false then
				set UI elements enabled to true
				if UI elements enabled is false then error "NO_PASSWORD"
			end if
			tell process "iWeb"
				if enabled of menu item "Publish to .Mac" of menu "File" of menu bar item "File" of menu bar 1 is true then
					click menu item "Publish to .Mac" of menu "File" of menu bar item "File" of menu bar 1
					set the loop_counter to 0
					repeat
						set the loop_counter to loop_counter + 1
						if the loop_counter is 60 then
							error "The script gave up trying to click the sheet OK button after one minute."
						else if exists sheet 1 of window 1 then
							click button "OK" of sheet 1 of window 1
							exit repeat
						else
							delay 1
						end if
					end repeat
				end if
			end tell
		end tell
	on error error_message
		display dialog error_message with title "Publishing Error" giving up after 15 buttons {"Stop", "Continue"} default button 2
		if button returned of the result is "Stop" or gave up of the result is true then
			tell me to quit
		end if
	end try
	return 900 -- 15 minutes
end idle

Model: Mac Pro
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

In a script saved with the Stay Open option, the run handler is the first thing to be executed (or the top-level statements, if there is no explicit run handler). Then, if the script is still running (i.e. it did not tell itself to quit, was aborted by the user, or encountered a run-time error), the idle handler is executed for the first time. The return value of the idle handler determines when it will be next executed.

Your script prompts the user for Stop/Begin in the on run handler. Since the on run handler does not do anything else (at least as far as I can tell), you can just delete the on run handler and it will not prompt the user on startup. The idle handler will then be executed without any user interaction (and again 15 minutes later, etc.).

After you have made the changes, if you are saving the script to a new filename, be sure to set the File Format to application (or application bundle), enable Stay Open, and disabled Startup Screen. If you use something besides the standard Script Editor, they might be called something slightly different.

Welcome, Doug. Just above the pane for creating a post here at MacScripter, you’ll see an AppleScript button. Clicking it produces a pair of applescript tags and if you put your script between them then others can just click on the link at the top of the script and it will download to their script editor. Alternatively, you can paste in the script, select all of it and then click the AppleScript button to enclose the selection in the tags.

I’ve done that to your original post, so if you click the edit button below your post, you’ll see how that works.