Something wrong with variable in AppleScript?

I am getting a “Pt token” (abbreviation of patient name) from Excel (clipboard -> store in KM variable) and trying to raise the window of Microsoft Word that contains that abbreviation.

AppleScript executed (after PtToken successfully set to 4-character abbreviation, such as “xxYy”) is

tell application "Keyboard Maestro Engine"
	set kmVarRef to variable "ptToken"
	set myPtToken to value of kmVarRef
end tell

tell application "System Events"
	tell application process "Microsoft Word"
		perform action "AXRaise" of (first window whose name contains myPtToken)
	end tell
end tell

This fails, with result of the script being

/var/folders/7v/c44ctl1534x81wp56k1vhz5r0000gn/T/Keyboard-Maestro-Script-CAAA5B90-90C5-4931-9C4E-2C1DBBEBB416:233:316: execution error: System Events got an error: Can’t get window 1 of application process "Microsoft Word" whose name contains "xxYy

Notice the lack of final quote! If on the other hand I hard-code “xxYy” instead of using the variable myPtToken, it works successfully, and the result is

action AXRaise of window stDo h3602061.docx of application process Microsoft Wordaction AXRaise of window xxYy 01234.docx of application process Microsoft Word


(PS: thank you so much for the help on other questions…I haven’t gotten around to implementing everything, and I have some things to share as well…just busy!)

Update: if I filter the clipboard to text only prior to assigning the ptToken to the clipboard, the AppleScript works!

Hey Aaron,

It’s better to talk to apps directly when possible.

tell application "Microsoft Word"
  set myDoc to (windows whose name contains "Junk")
  if length of myDoc = 1 then
    set myDoc to item 1 of myDoc
    activate object myDoc
  end if
end tell

* Note: the value for the window name is case-sensitive in Word (unlike most apps).

Word and Excel have private clipboard formats, so that’s why you’re having to filter the clipboard.

It’s better to avoid using the clipboard when possible.

tell application "Microsoft Excel"
  set ptToken to value of selection
end tell
if ptToken ≠ "" then
  # Continue
end if


1 Like

Sometimes applications when they copy to the clipboard dopy different text to the clipboard for the rich text variant and the plain text variant. In theory, the rich text text should be exactly the same as the plain text text. But you know what they say about Theory and Practice, right:

In Theory, Theory and Practice should be the same, but in Practice they are not.

So sometimes, the rich text text includes extra characters, like list markers, or weird line endings, or whatever else the application designer or system software thought was important to displaying the text in the same manner as it was originally displayed.

By filtering the clipboard and removing styles, what you are really doing is reading the plain text text from the clipboard, scrapping everything else, and writing a fresh new clipboard that has only the plain text text.

Usually this step is unnecessary, because usually the actual text in both the rich and plain text variants on the clipboard is the same - but when copying from heavily styled text such as Word or PDF, it can make an important difference.

1 Like