Error handling of AppleScript results in KM variables

A lot of my macros use Applescript and their results. I then need the results in IF-THEN macros.
As a standard, the macros containg the applescript codes save their results to a variable named "KMV_ASR", which a standard error-checking macro scans for possible strings like "err", "exception" and similar things. The respective macros have a checkmark for "include errors".

Now there’s one problem. Most of my scripts are running smooth, but return before their stored result a first line like this one:
2020-05-13 18:11:57.821 osascript[13574:7425605] ApplePersistence=NO
where I only would need the figure "2".

I remember that this "ApplePersistence" thing was a seldom liked feature introduced with Autosave in Lion, which you could turn off on a per app basis with "defaults write ApplePersistence -bool no".
But a quick check with "defaults read ApplePersistence" did not show a key/value pair.

This thing drives me mad, as I do not find a way to circumvent this ApplePersistence thing. Removing the checkmark for "include errors" would obviously render my standard error-checking macro useless, but with this Persistence thing, the output of the variable is also a nonvalue….

Anbody has an idea? Thanks for a hint.

As of KM9 if your AppleScript throws an error, then the KM Execute AppleScript will abort the Macro. So what I do now as of KM9 is to have my AppleScript display a notification when there is an error, and then throw an error that gets trapped by KM. It has been working very well. That way I do NOT need a post-script error handler in KM.

Here's my AppleScript error handler:

on error errMsg number errNum
	if errNum = -128 then ## User Canceled
		set errMsg to "[USER_CANCELED]"
	end if
	set scriptResults to "[ERROR] [MACRO CANCELLED] " & errMsg
	set currentApp to path to frontmost application as text
	tell application currentApp
		display notification errMsg ¬
			with title ("SCRIPT:  " & ptyScriptName & " " & ptyScriptVer) ¬
			subtitle "[ERROR] MACRO CANCELLED " sound name "glass"
	end tell
	--»	THROW ERROR ~~~~~~~
	error scriptResults

Be sure to set your KM Execute AppleScript like this:


1 Like

Hmm..., but if I run the code in a scripteditor window it does not throw an error. This ApplePersistance thing seems to exist only in KM.
KM records this ApplePersistance notice even when a very simple applescript script (ie. "set x to 1")
is executed.

Moreover, I mostly run my code via an applet or a script (.scpt), KM only plays the "Caller" and processes eventual results. That means I would have to add this error notification to each of my script files.... what a horrible idea (I run hundreds of scripts).

NB: I prefer scripts or applets because I can use them also in other Macro apps like QuicKeys, which is still my preferred macro app, especially when running OSX 10.6.8 which is still my preferred OSX (a solid workhorse without all this chichi of ios, and very very fast).

Must be something specific to your environment and/or setup/use of AppleScripts.
I have been running AppleScripts (both as text and from a .scpt file) in KM for years, and I've never seen that error.

It would be best for you to upload your macro with the AppleScript that consistently causes the error. Then we could test it on our Macs.

Actually, I consider it a best practice to always include an error handler in every script I write. IMO, the last thing you want is for the script to fail and the user has no idea what happened.

If you want to have a KM error handler you can, but you still need for your AppleScript to return the error message.

If you are run macOS 10.6.8, then you are also running an older version of KM.
I really can't offer much help for that very old OS.
I'm running Keyboard Maestro 9.0.5 on macOS 10.14.6 (Mojave).
I find it very fast and have very few problems that are caused by the OS.

osascript is reporting that on the stderr error stream.

Searching around for ApplePersistence find the old du man page:

Revision files

In OSX Lion and greater, folders under /.DocumentRevisions-V100 are used to store previous versions of auto-saved documents.
This can be turned off for each app (defaults write -app 'ApplicationNametoDisable' ApplePersistence -bool no)
Most current mac applications do manage this sensibly and delete older version files automatically.
Old version files can be deleted in bulk with OnyX (Cleaning>Misc>Delete 'Automatically saved versions of Documents').

So I would guess at some point on your Mac, ApplePersistence was set to no to turn off revision files, and for whatever reason osascript is reporting that.

Try the Terminal command:

defaults read -g ApplePersistence

That should give the error The domain/default pair of (kCFPreferencesAnyApplication, ApplePersistence) does not exist, but maybe it is set there. Or it may be set in some other place, under a specific application, eg:

defaults read com.stairways.keyboardmaestro.engine ApplePersistence

(try with various other Apple IDs).

Anyway, you will have to find where that setting is set, and then delete it, and then probably restart.

Last thing first (for once): I have a machine for nearly every major OSX: from macos 7 to 9, then from Tiger up to Snow Leo, and from Yosemite to High Sierra. Up to Capitan, QuicKeys is installed on every machine since macos 9, but KM (in its version v905) only as from Capitan.
I mentioned this 10.6.8 only to illustrate, that I use different macro apps and try to use AppleScript the way to be compatible with as many OSX’es as possible. That said, I also use QuicKeys in Capitan, sometimes slow, but it works, including all my AppleScript codes.

I also mentioned it to illustrate, that this ApplePersistence notice is only shown in KM, not elsewhere. All AppleScript code (be it in .scpt files, applets or contained as text in KM run smooth).

It’s literally ALL codes. Here’s a simple example:

»set PrefF to (path to "pref" from user domain) as text«
It doesn’t matter if I run it as an applet or as script, or insert it in a macro app like QuicKeys, I get each time the correct result:
-->"name of startup disk:Users:name of home folder:Library:Preferences:"

So far so good. But if I insert it in a Keyboard Maestro macro, I get a twoliner:
-->"2020-05-14 18:54:16.366 osascript[2085:1241813] ApplePersistence=NO
name of startup disk:Users:name of home folder:Library:Preferences:"

Note that the action option are set like this:
Bildschirmfoto 2020-05-15 um 08.43.29

Nota bene: If I disable the "include Errors" option, then KM9 stores only the correct result, but I loose the ability to catch errors if such occurs.
With that informations I googled the internet up and down but have found no solution. But I stumbled also over the following possibility:
»osascript -e "set PrefF to (path to "pref" from user domain) as text"«
to be inserted in Terminal. And voilà, now the result is the same as in KM9.

That makes me concluding, that this is more an osascript behavioural thing than KM's problem.
I may try to put the question also to the applescript user list, there's maybe supplement knowledge around.

Thanks for your answer, Peter.

As I stated already in my initial post, I remember that this "ApplePersistence" thing was a seldom liked feature introduced with Autosave & Revisions in Lion and that one could turn it OFF/ON on a per app basis with "defaults write ApplePersistence -bool no/yes".

And indeed, I usually do turn it OFF for several applications on all OSX'es since Lion because I do not like this feature. For that reason, I recently have made a clean new installation (without MA) of 10.11.6 to get rid of possible garbage from earlier systems to cope with that problem without touching that feature. And AFAIK, this works only on an application base.

Anyhow, I had already done a quick check for "defaults read ApplePersistence" before posting, and it did not show any key/value pair for the Editor, but I cannot check this for osascript, as this seems to work only for apps, not binaries.

I now tried your hint

but this resulted in a simple "0".

Problem solved:
Concluding from your post, it appears that this ApplePersistence setting should be in a plist file. I scanned all plist files in ~Preferences for it, and voilà, ".GlobalPreferences.plist" file showed up containing that key.
Deleted, restarted: and the problem is gone! Hurrah.
Thanks a lot Peter, that was valuable help.

The -g means “global”, so it essentially sets it for all applications.

The correct way to remove it would be:

defaults delete -g ApplePersistence

You restarted, so editing the plist is generally fine, but in my experience the system caches the plists quite a lot these days so editing them manually is not recommended - changes will often be ignored or reverted.

I'm glad it is resolved.

1 Like