osacompile changing the AppleScript output so it won't run

送分小仙女□ 提交于 2019-12-23 04:00:11

问题


I have a semi-long AppleScript that I run each morning to launch all of my apps etc. One of the things that it does is launch a few apps and then immediately minimize them. When I paste the .applescript source into Script Editor and run it, everything works fine:

-- snip:
tell application "Mail"
    launch
    minimize(window 1) of me
    check for new mail
end tell
-- 'minimize' defined as:
on minimize(w)
    set the miniaturized of w to true
end minimize

But when I compile the AppleScript source as follows:

osacompile -o ~/Library/Scripts/myscript.scpt myscript.applescript

... the compiler munges minimize to be:

on minimize(w)
    set |miniaturized| of w to true
end minimize

And I get this error:

error "Mail got an error: Can’t make |miniaturized| of window id 30936 into type reference." number -1700 from |miniaturized| of window id 30936 to reference

Anyone have any clue what I'm doing wrong here? For purposes of version control, I need to run the scripts through osacompile.

UPDATE: To clarify, what seems to be happening is that Script Editor is compiling the method differently than osacompile on the command line. Is it known whether they compile different (e.g., using scope inferences or some such thing)?


回答1:


There is nothing wrong with your code - I suspect this is a bug in osacompile and I suggest you file a bug report with Apple - as I've done.

You can verify that your code works correctly by using AppleScript Editor to save it as a *.scpt file directly and then running it with osascript. [Updated] By contrast, passing the *.applescript source-code file directly to osascript does exhibit the problem.

There is no good reason I can think of for AppleScript Editor-based compilation to work differently from osacompile (and on-demand compilation in osascript), and the former's behavior is the expected and desired one in this case.

There are 2 workarounds:

  • Enclose the reference to miniaturized in a using terms from application "System Events" block: This is a generic workaround that should work with windows from any AppleScriptable application.

    on minimize(w)
        using terms from application "System Events" # WORKAROUND
             set miniaturized of w to true
        end using terms from
    end minimize
    
  • Inline the miniaturization command instead of calling a subroutine:

    set miniaturized of window 1 to true
    



回答2:


on minimize(w)
    set the miniaturized of w to true
end minimize

This is not correct. You have "miniaturized" outside any application tell block of code, which means that it's an applescript command. It isn't an applescript command though. It's a command of Mail and other applications. It's amazing that it works properly in AppleScript Editor. It really shouldn't. When you have a command that doesn't make sense applescript is good enough to try to make sense of it and sometimes it can overcome your coding mistake. Obviously osacompile can't overcome your coding mistake.

So the way to fix the osacompile issue is to eliminate the coding mistake. You need to have the miniaturized command inside of an application tell block of code.

To see what I mean open the "standard additions" applescript dictionary and try to find the miniaturized command. you won't find it. Now open Mail's applescript dictionary and you will find it. Thus the command belongs to Mail, not applescript.



来源:https://stackoverflow.com/questions/8835496/osacompile-changing-the-applescript-output-so-it-wont-run

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!