When Pages become Script Editor

Hello

In a script supposed to treat thousands of documents with Pages and LibreOffice, every 20 messages, I decided to clear the list of recent items in LibreOffice, quit Pages and LibreOffice then restart them to reduce memory use.

It works well but when I looked at the Events log I saw some surprising lines.

The code is :

# Quitte les deux applications pour leur faire libérer la mémoire occupée
tell application id "com.apple.iWork.Pages" to quit
tell application "LibreOffice" to quit
# Relance les applications pour qu'elles soient disponibles pour lr document suivant.
tell application id "com.apple.iWork.Pages" to activate
tell application "LibreOffice" to activate

I trigger Pages via its application id so that the script doesn’t change according to the version in use which may be Pages v.4.x or v.5.x

In the log the tasks are reported as :
[format]tell application “Script Editor” # was supposed to be “Pages”
quit
end tell
tell application “LibreOffice”
quit
→ error number 0 # non fatal error
end tell
tell application “Script Editor” # What is it doing here ?
activate
end tell
tell application “Pages”
activate
end tell
tell application “Script Editor” # What is it doing here ?
activate
end tell
tell application “LibreOffice”
activate
end tell
[/format]

I would be interested if somebody may give an explanation to this behavior.

Yvan KOENIG running El Capitan 10.11.6 in French (VALLAURIS, France) dimanche 18 septembre 2016 16:35:09

I suspect you need to add some delay after quitting the apps. It looks like the target application is not being found, and the commands are therefore falling through to the current application (Script Editor).

Hello Shane

The instruction : [format]tell application id “com.apple.iWork.Pages” to quit[/format]
is reported as :
[format]tell application “Script Editor” # was supposed to be “Pages”
quit
end tell[/format]
although it’s the first one to be executed - after a lot of other tasks.
In the real script there are also numerous tasks between the two ‘qui’ and the two ‘activate’.
The job is perfectly done but I don’t understand why the log reports Script Editor.

After posting my question, I discovered that

# Quitte les deux applications pour leur faire libérer la mémoire occupée
tell application "Pages" to quit
tell application "LibreOffice" to quit
delay 1
# Relance les applications pour qu'elles soient disponibles pour lr document suivant.
tell application "Pages" to activate
tell application "LibreOffice" to activate

is reported as :
[format]tell application “Pages” # this time it’s Pages
quit
end tell
tell application “LibreOffice”
quit
end tell
tell application “Script Editor” # this one is always puzzling
activate
end tell
tell application “Pages”
activate
end tell
tell application “Script Editor” # this one is always puzzling
activate
end tell
tell application “LibreOffice”
activate
end tell
[/format]
in the events log.

If I code :

quit application "Pages"
quit application "LibreOffice"
delay 1
# Relance les applications pour qu'elles soient disponibles pour lr document suivant.
activate application "Pages"
activate application "LibreOffice"

or

quit application id "com.apple.iWork.Pages"
quit application "LibreOffice"
delay 1
# Relance les applications pour qu'elles soient disponibles pour lr document suivant.
activate application id "com.apple.iWork.Pages"
activate application "LibreOffice"

I retreive :
[format]tell application “Script Editor”
quit
end tell
tell application “LibreOffice”
quit
end tell
tell application “Script Editor”
activate
end tell
tell application “Pages”
activate
end tell
tell application “Script Editor”
activate
end tell
tell application “LibreOffice”
activate
end tell[/format]

Same behavior if I replace application “LibreOffice” by application id “org.libreoffice.script”

Yvan KOENIG running El Capitan 10.11.6 in French (VALLAURIS, France) lundi 19 septembre 2016 09:00:24

I get similar logs when substituting Numbers for LibreOffice in Yvan’s script immediately above.

The “Script Editor” business only appears when there’s communication with another application in between the ‘quit’ and ‘activate’ commands:

-- With Pages and Numbers both already open:
tell application "Pages" to quit
tell application "Numbers" to get window 1
delay 1
tell application "Pages" to activate

(* Event log:
tell application "Pages"
	quit
end tell
tell application "Numbers"
	get window 1
end tell
tell application "Script Editor"
	activate
end tell
tell application "Pages"
	activate
end tell
*)

When there’s no intervening communication:

tell application "Pages" to quit
--tell application "Numbers" to get window 1
delay 1
tell application "Pages" to activate

(* Event log:
tell application "Pages"
	quit
	activate
end tell
tell application "Pages"
	activate
end tell
*)

. which is equally interesting. It seems to be a harmless Script Editor logging quirk. Nothing to do with the targeted applications.

Running it in ASObjC Explorer or Script Debugger gives you more clue what’s going on. Script Debugger gives the best view.

So in Nigel’s first script, you can see that Pages gets told to quit, then Numbers gets asked for window 1. Then when Pages is told to activate, the log shows the result as:

no eligible process with specified descriptor (procNotFound:-600)

In other words, AppleScript tries to prepare an event to send to what AppleScript assumes is still the Pages process, but which is no longer running. AppleScript must somehow recognize this error, and treats it as a signal to abort sending the event and instead construct a new event containing a proper target, and to send that. That results in a second activate in both logs.

The reason Script Editor shows itself as target of the first activate is that when it finds no target in an event, it falls back to itself as the default target in order to get proper source to include in the log. If it did not, other stuff would not be logged correctly. And Explorer does the same thing.

In Explorer’s case, the error is also logged but by number only:

  -->	error number -600

Look that up in MacErrors and it says no eligible process with specified descriptor.

Script Editor is not logging the error. It looks like it only logs errors generated from replies to events, and ignores the (relatively rare) errors generated in constructing outgoing events.

Thanks Nigel

Your message pushed me to make other tests with other applications giving funny results.

tell application "Mail" to quit
tell application "TextEdit" to quit
delay 1
tell application "Mail" to activate
tell application "TextEdit" to activate

give :
[format]tell application “Mail”
quit
→ error number 0 # non fatal one
end tell
tell application “Script Editor”
quit
end tell
tell application “Script Editor”
activate
end tell
tell application “Mail”
activate
end tell
tell application “Script Editor”
activate
end tell
tell application “TextEdit”
activate
end tell[/format]

Then I changed the order of the applications :

tell application "TextEdit" to quit
tell application "Mail" to quit
delay 1
tell application "TextEdit" to activate
tell application "Mail" to activate

[format]tell application “Script Editor”
quit
end tell
tell application “Mail”
quit
end tell
tell application “Script Editor”
activate
end tell
tell application “TextEdit”
activate
end tell
tell application “Script Editor”
activate
end tell
tell application “Mail”
activate
end tell[/format]

When I ran the 1st version in Shane’s ASObjC Explorer, I got :
[format]14:20:10,925 tell application “Mail” to quit
14:20:11,242 [1] tell application “Mail” to quit
14:20:11,244 tell application “TextEdit” to quit
14:20:11,245 [2] tell application “TextEdit” to quit
14:20:12,246 [3] delay 1
14:20:12,248 tell application “ASObjC Explorer 4” to activate
→ error number -600
14:20:13,493 tell application “Mail” to activate
14:20:13,626 [4] tell application “Mail” to activate
14:20:13,628 tell application “ASObjC Explorer 4” to activate
→ error number -600
14:20:14,040 tell application “TextEdit” to activate
14:20:14,145 [5] tell application “TextEdit” to activate[/format]

With the 2nd one in Shane’s ASObjC Explorer I got :
[format]14:22:58,539 tell application “TextEdit” to quit
14:22:58,556 [1] tell application “TextEdit” to quit
14:22:59,240 tell application “Mail” to quit
14:22:59,241 [2] tell application “Mail” to quit
14:23:00,242 [3] delay 1
14:23:00,242 tell application “ASObjC Explorer 4” to activate
→ error number -600
14:23:00,578 tell application “TextEdit” to activate
14:23:00,680 [4] tell application “TextEdit” to activate
14:23:00,681 tell application “ASObjC Explorer 4” to activate
→ error number -600
14:23:01,926 tell application “Mail” to activate
14:23:02,042 [5] tell application “Mail” to activate[/format]

So, I guess that the problem isn’t in the Editor but in AppleScript itself.

Maybe Shane will find what is playing the fool :wink:

Yvan KOENIG running El Capitan 10.11.6 in French (VALLAURIS, France) lundi 19 septembre 2016 14:25:22

I have a similar issue. This code:

		do shell script "echo " & loops & quoted form of ("	") & (current date) & " >> /private/var/tmp/loop-log.tsv"
		if machineName is "15" then

			try
				log "quit Warp"
				tell application "Cloudflare WARP" to quit
				delay 3
			end try
			try
				tell application "Cloudflare WARP" to activate
			end try

generates this output:


tell current application
	current date
		--> date "Thursday, 3 August 2023 at 15:31:19"
	do shell script "echo 1'	'Thursday, 3 August 2023 at 15:31:19 >> /private/var/tmp/loop-log.tsv"
		--> ""
	(*quit Warp*)
end tell
tell application "Script Editor"
	quit
		--> error number 0
	activate
end tell
tell application "Cloudflare WARP"
	activate
end tell

I tell Cloudflare WARP to quit but Script Editor reports that it is itself that is told to quit. And why is there an activate just after Script Editor is told to quit? It is not in the code.

Note that the log "quit Warp" is “interleaved” with the previous (implicit) tell-statement.