hammerspoon icon indicating copy to clipboard operation
hammerspoon copied to clipboard

“hs.osascript.applescriptFromFile” error: “LuaSkin: Unable to initialize script"

Open roeybiran opened this issue 5 years ago • 11 comments

Hi everyone,

I’ve been trying to execute many different .scpt files, and all yield the same error:

ERROR:   LuaSkin: Unable to initialize script: {
    NSLocalizedDescription = "A real number can\U2019t go after this identifier.";
    NSLocalizedFailureReason = "A real number can\U2019t go after this identifier.";
    OSAScriptErrorAppAddressKey = "<NSAppleEventDescriptor: null()>";
    OSAScriptErrorBriefMessageKey = "A real number can\U2019t go after this identifier.";
    OSAScriptErrorMessageKey = "A real number can\U2019t go after this identifier.";
    OSAScriptErrorNumberKey = "-2740";
    OSAScriptErrorOffendingObjectKey = "<NSAppleEventDescriptor: 'utxt'(\"real number\")>";
    OSAScriptErrorRangeKey = "NSRange: {0, 13}";
}

All of those scripts compile and run fine when executed through other tools, but even a script that merely contains return "foo" fails in a similar fashion. Clearly I’m doing something wrong, any help would be appreciated!

P. S. — I couldn’t be grateful enough to those involved in this amazing project. It changed my computing life!

roeybiran avatar May 07 '19 16:05 roeybiran

Does something like this work?

local path = "/path/to/file"
local file = io.open(path, "r")
local data = file:read("*a")
file:close()
hs.osascript.applescript(data)

latenitefilms avatar May 08 '19 10:05 latenitefilms

Hi @latenitefilms, Nope, I still get the same error. Meanwhile, I came up with a workaround, but I fear that it would cost me in performance:

function execAppleScript(scpt, args)
  local scpt = 'run script ((POSIX file "' .. scpt .. '") as alias)'
  if args ~= nil then
    scpt = scpt .. ' with parameters ' .. args
  end
    hs.osascript.applescript(scpt)
end

Generally, what method of running AppleScript code (whether string or file, and optionally with one or more parameters) would be considered to be the speediest?

roeybiran avatar May 08 '19 14:05 roeybiran

I had a similar error when running applescript via Hammerspoon, I noticed this when using the Spotify extension which runs applescript. Updating my Mac resolved the issue.

On MacOS version 10.14.4 (18E226), Hammerspoon version 0.9.75, in the Hammerspoon Console:

> hs.osascript.applescript('tell application "Spotify" to get player state')
2019-07-25 10:03:15: ********
2019-07-25 10:03:15: 10:03:15 ERROR:   LuaSkin: Unable to initialize script: {
    NSLocalizedDescription = "A identifier can\U2019t go after this identifier.";
    NSLocalizedFailureReason = "A identifier can\U2019t go after this identifier.";
    OSAScriptErrorAppAddressKey = "<NSAppleEventDescriptor: null()>";
    OSAScriptErrorBriefMessageKey = "A identifier can\U2019t go after this identifier.";
    OSAScriptErrorMessageKey = "A identifier can\U2019t go after this identifier.";
    OSAScriptErrorNumberKey = "-2740";
    OSAScriptErrorOffendingObjectKey = "<NSAppleEventDescriptor: 'utxt'(\"identifier\")>";
    OSAScriptErrorRangeKey = "NSRange: {34, 12}";
}
2019-07-25 10:03:15: ********
false	nil	table: 0x600011a88300

Same error when running the applescript using applescriptFromFile as well, although I didn't try the workaround above.

After updating to MacOS version 10.14.6 (18G84) and no change in the Hammerspoon install, it's as expected:

> hs.osascript.applescript('tell application "Spotify" to get player state')
2019-07-31 10:13:06: -- Loading extension: osascript
true	kPSp	'kPSp'

Either the update solved it for me or simply restarting did the trick.

I'm not certain this is the same problem because the error message is slightly different, but that might be due to the applescript I've been running being different. It's probably worth checking for any MacOS updates if you're still seeing this error though.

dancorne avatar Jul 31 '19 09:07 dancorne

I get the same error, either using applescriptFromFile or with applescript(data) like in the second comment. I'm on 10.14.6 and updated hammerspoon to latest. Even after restart of the system I'm still getting the same error.

When I run my script via applescript("...") where ... is my actual text code (using \n to break lines since it's a 3 line script) it's working. When I dump data after reading from file, it's not plain text. Maybe that's the problem?

kassi avatar Nov 06 '19 08:11 kassi

@kassi Does this work for you?

hs.osascript.applescript([[display dialog "this is a test"]])

latenitefilms avatar Nov 06 '19 11:11 latenitefilms

@asmagill - I wonder if this is a text encoding issue too?

latenitefilms avatar Nov 06 '19 11:11 latenitefilms

@latenitefilms yes, that's working fine.

kassi avatar Nov 06 '19 16:11 kassi

@kassi - Can you share what your data is?

latenitefilms avatar Nov 06 '19 21:11 latenitefilms

I'm also getting this error on 10.15.2, even when I've commented out the entire applescript (or changed the file contents to just display dialog "foo".

It runs fine with the hs.osascript.applescript([[display dialog "this is a test"]]) test from above, and the original script (and test dialog) both run fine from Script Editor.

UPDATE: It works fine if I "save as" a "text" file (.applescript) instead of a compiled / runnable script (.scpt).

n8henrie avatar Feb 06 '20 15:02 n8henrie

Hi guys, I found a simple solution to this problem. Lua can't read AppleScript files when they have .scpt extension. But it CAN read .applescript extensions which are basically just text files.

To export the file to .applescript extension open the script in Script Editor then File > Export > File Format: Text. Now, save it to ~/.hammerspoon.

Now, invoke the script with hs.osascript.applescriptFromFile("yourfile.applescript")

Here's and example in context:

streamcopy = hs.menubar.new()
streamcopy:setTitle("⬇︎")

function streamcopyClicked()
    hs.osascript.applescriptFromFile("streamCopy.applescript")
end

if streamcopy then
    streamcopy:setClickCallback(streamcopyClicked)
end

Hope this works for you as it did for me :)

I was getting the same errors as you all before changing the extension from .scpt to .applescript

Screen Shot 2021-10-16 at 9 18 14 PM

mcstrassell09 avatar Oct 17 '21 01:10 mcstrassell09

This is weird! I've just recreated the file from scratch! And now it's working! Very odd!

-- Write WAN IP hs.hotkey.bind({"cmd", "shift", "ctrl"}, "W", function() hs.osascript.applescriptFromFile("/Users/Liv/Library/Mobile Documents/com~apple~ScriptEditor2/Documents/WanIP.scpt") end)

-- Open Firefox MSP ESET - Firefox hs.hotkey.bind({"cmd", "shift", "ctrl"}, "R", function() hs.osascript.applescriptFromFile("/Users/Liv/Library/Mobile Documents/com~apple~ScriptEditor2/Documents/Eset_MSP.scpt") end)

The wan IP was working great, the problem was with the second script. But as I said, after recreating the file was working as a charm! 😁👍

Boshimanu avatar Apr 12 '22 10:04 Boshimanu