Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

iConcept

macrumors member
Original poster
Sep 1, 2009
46
32
UK
Hi,

I'm trying to create an Applescript which runs as an application. When run, it prompts the user for a number of hours, then waits this number of hours to launch a specific application, for instance, TextEdit.

For some reason (I think since Yosemite), each first time I open or compile in Applescript, or the application once saved, it starts the target application immediately, before it's even prompted the user. The process repeats if Applescript is closed down and opened again. It never used to do that prior to Yosemite, then seemed to start all of a sudden.

Code is as follows:

Code:
set x to text returned of (display dialog "Open TextEdit in {x} hours?" with title "Input value..." default answer "0" buttons {"Cancel", "Start"} default button 2)
set y to x
set x to (x * 60) * 60
if x > 0 then
	set myDate to current date
	set myNewDate to myDate + (y * hours)
	set myTime to time string of myNewDate
	set myParts to words of myTime
	set myDisplay to (item 1 of myParts) & ":" & (item 2 of myParts)
	display dialog "TextEdit will start at " & myDisplay with title "Starting at..." buttons {"Cancel", "Confirm"} default button 2
	display notification "Starting TextEdit at " & myDisplay
	set input to x
	delay input
	tell application "TextEdit" to launch
else
	display dialog "Cannot be 0 or null." with title "Error..." with icon stop buttons {"Ok"} default button 1
end if

I've tried using a block tell/end tell and using activate rather than launch but I get the same thing happening. Also I've tried using a sub-routine with the same result. If I comment out the tell statement, the problem naturally doesn't occur.

I only want the application to launch once the user has submitted the input and assumed the if statement would not execute until this was done. Can anyone advise if/where I am going wrong? I'm aware the code may be messy, and there are likely better ways to do things, but I've not got a lot of experience with Applescript.

Thanks in advance for any help.
 
You can try :

Code:
launch application "TextEdit"
--or
do shell script "open -a /Applications/TextEdit.app"
 
You can try :

Code:
launch application "TextEdit"
--or
do shell script "open -a /Applications/TextEdit.app"

Thanks Kryten2, you're a smeggin' genius. The first option seems to have worked, so didn't bother with the second.

Just shows that simpler is sometimes better.
 
Thought I had another issue. When saved as an application, the above code would execute immediately, ignoring the delay. Seems this is a bug with Yosemite which accepts the delay when run as a script, but ignores it when run as an application.

As a workaround, I found the following from Chris Page on StackExchange:

Code:
on delay duration
	set endTime to (current date) + duration
	repeat while (current date) is less than endTime
		tell AppleScript to delay duration
	end repeat
end delay

This has done the trick so I thought I'd share in case anyone else has a similar problem.
 
The problem is a bug with the delay command itself (it immediately times out if you move the mouse, for example), so you are better off not using it at all. You also need to make sure not to lock the script into a tight repeat loop, since the application's user interface will get blocked.

A workaround from the mailing list that keeps the UI responsive:
Code:
on delay delayTime
	set theRepeat to delayTime * 10 div 1
	repeat theRepeat times
		do shell script "sleep 0.1"
	end repeat
end delay
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.