on launched vs on open and error -1708 [should quit]

  1. I have a on launched theObject handler and a on open theFiles handler to handle both double clicking and drag and drop. In regular Script Editor the equivalent handlers are on run and on open theFiles.

Within Script Editor, the on run handler presents a dialog that warns the user that this app is drag and drop only. With Sript Editor, a drag and drop accesses on open theFiles and, so, no such warning happens. This is exactly what should happen.

Using AppleScript Studio, upon double clicking, the on launched theObject handler presents the same warning dialog (attached to the opening window).

However, there’s the rub; namely, drag and drop will access the same on launched theObject handler and present the same dialog warning and obviously I don’t want such a warning when doing the drag and drop.

I am presenting all the code not for this problem, but for the error -1708 problem:



property gSaved : false

on displayDialog(theMsg, theButtonsArray, theDefaultButton, theGivingUp, theIcon, theWindow)
	
	script
		property itsMsg : theMsg
		property itsButtonsArray : theButtonsArray
		property itsDefButton : theDefaultButton
		property itsGiveUp : theGivingUp
		property itsIcon : theIcon
		property itsWindow : theWindow
		property dd : null
		property theResult : ""
		
		on showItself()
			set dd to display dialog itsMsg buttons itsButtonsArray default button itsDefButton ¬
				giving up after itsGiveUp with icon itsIcon attached to window itsWindow
		end showItself
		
		on dialog ended theObject with reply dd
			
			if (gave up of dd) then
				set theResult to itsDefButton
			else
				set theResult to (button returned of dd)
			end if
			
		end dialog ended
		
	end script
end displayDialog

on launched theObject -- the application
	
	beep
	
	set launchDlg to ¬
		displayDialog("Drop one Microsoft Excel Spreadsheet on me!", {"OK"}, "OK", ¬
			15, "stop", "Calculate Medical")
	tell launchDlg to showItself()
	
	tell theObject to continue quit
	
end launched

on allSaved()
	
	if (not gSaved) then
		beep
		
		set continueDlg to ¬
			displayDialog("You have not finished calculating your Spreadsheet." & return & ¬
				"Do you wish to continue calculating?" & return & return, ¬
				{"Yes", "No"}, "Yes", 15, "stop", "Calculate Medical")
		tell continueDlg to showItself()
		
		set gSaved to (theResult of continueDlg) is "No"
	end if
	
	return gSaved
	
end allSaved

-- clicking the "OK" button
on clicked theObject
	
	-- display dialog name of theObject
	if allSaved() then tell window of theObject to close
	
end clicked

-- closing the window
on should close theObject
	
	(*
		If we get here via on clicked Handler, allSaved()
		doesn't present a second dialog because the
		global gSaved has been set to true.
		
		If we just close the window, then allSaved()
		does present a new dialog if gSaved is false.
		
		Ditto applies to on should quit.
	*)
	
	return allSaved()
	
end should close

-- CMD-Q
on should quit theObject -- the application
	
	return allSaved()
	
end should quit


on should quit after last window closed theObject -- after
	
	return true
	
end should quit after last window closed

on open theFiles
	
	if ((count of theFiles) > 1) then
		beep
		
		display dialog "Drop only one Microsoft Excel Spreadsheet on me!" buttons {"OK"} ¬
			default button "OK" giving up after 15 ¬
			with icon "stop" attached to window "Calculate Medical"
		
		quit
	else
		set theWorkbook to item 1 of theFiles
		set fileCreator to file creator of (info for theWorkbook)
		set fileType to file type of (info for theWorkbook)
		
		if (fileCreator is not equal to "XCEL" or fileType does not contain "XLS") then
			beep
			
			display dialog "Drop only a Microsoft Excel Spreadsheet on me!" buttons {"OK"} ¬
				default button "OK" giving up after 15 ¬
				with icon "stop" attached to window "Calculate Medical"
			
			quit
		else
			calculateWorksheet(theWorkbook)
			saveWorksheet()
				
			set gSaved to true
		end if -- else dropped an Excel Spreadsheet as prescribed
	end if -- else just one dropped file as prescribed
	
end open


For the life of me, I see nothing in the above code that could generate the -1708 error, which Rosenthal’s book states is “ doesn’t understand the message”

I did check under “File Owner” Inspector and I have checks for Launched, Open, Should Quit and Should Quit after last window closed.

For the button “Quit” of the main window I have checked “Action:clicked” and Key" and for the window, I have checked Should Close.

Have you considered letting the user choosing an excel file and passing that to the open handler?

Which line is causing the error?

Isn’t that what the user does with drag and drop?

Cannot find it in my code; for example:


on should quit theObject -- the application
	
	-- return allSaved() -- original

	set isSaved to allSaved()
	display dialog isSaved
	return isSaved
	
end should quit

The attached dialog sheet from allSaved() drops down, then my display dialog isSaved shows up. I first dismiss the display dialog isSaved followed by dismissing the dialog sheet drop down by clicking “No” which, in turn, evaluates isSaved to true. At this point, application should quit; however, the -1708 error dialog shows.

Based on this, my guess is that the error is generated behind-the-scenes and after return isSaved.

I meant as an alternative to drag and drop. For a non-Studio app, I would use something like this:

on run
	choose file without invisibles
	open {result}
end run

on open theseItems
	-- whatever	
end open

Try something like this:

property gSaved : false

on should quit theObject
	if not gSaved then
		display dialog ¬
			("You have not finished calculating your Spreadsheet." & return & ¬
				"Do you wish to continue calculating?" & return & return) ¬
				buttons {"Quit", "Continue"} default button 2 ¬
			giving up after 15 with icon warning attached to window "main"
	end if
	
	return gSaved
end should quit

on dialog ended theObject with reply withReply
	if (button returned of withReply is "Quit") or (gave up of withReply is true) then
		set gSaved to true
		quit
	end if
end dialog ended

Side note: Is your “launched vs. open” thing supposed to be a question? Please use one topic for each question in the future.

Bruce:

I had already developed a non-Studio app with on run + on open handlers that work just great.

After reading all the stuff about Studio, plus the fact that it utilizes windows, plus Menus, etc. I decided to dive right in and am trying to learn Studio via trying to convert my working non-Studio app to Studio.

My current goal is to try to combine Studio’s on launched and on open within the same app … as long as I can resist the temptation to simultaneously delve into Menus, About Windows, Help Windows, CMD-key equivalents etc. I am my own worst enemy in this matter.

I am starting to dig deep into Apple’s docs, “StudioBuildingApps” and “StudioReference” – boy do I wish Rosenthal would write a book just on Studio, rather than throw in 100 pages or whatever into his AppleScript classic (which I thoroughly endorse as the best book for me on regular AppleScript)

Later and many thanks …

Let me attempt to ask just one question here, rather than mixing it up with other stuff:

How can I tell when the app has been double-clicked or had a file dragged and dropped on it?

I’ve looked at:


on on awake from nib theObject

on will finish launching theObject

on launched

on open theFiles

Studio’s reference pdf states, I believe, that the above list is in the proper execution order and I have confirmed it via my coding.

The open handler and the on launched handler are called when app is activated via drag-and-drop. What I want to do is at the get-go determine if the call to on launched handler came from double-clicking or drag-and-drop.

My on launched handler calls display open panel (followed by calling on open) which I do not want if activated via drag-and-drop.

Thanks in advance.