Re: tell app, name in string var
Re: tell app, name in string var
- Subject: Re: tell app, name in string var
- From: Christopher Nebel <email@hidden>
- Date: Fri, 20 May 2011 13:33:29 -0700
Strictly speaking, your script *does* have syntax problems. (Or did, anyway.) AppleScript changes its interpretation of identifiers (and multi-word identifiers) based on the current "tell" target, as evaluated at compile time. (Essentially, which application are you "tell"-ing?) If the target isn't a compile-time constant, such as your variable, then AppleScript is forced to presume that only the built-in identifiers are valid, which means nothing custom to your putative target, in this case Excel, will compile. The obvious way around that is to use "using terms from", which is a purely compile-time service -- it informs the compiler that this is the application you mean. It has no effect at run-time.
The better question, and what Luther might have been getting at, is why are you trying to use a variable application name with application-specific terminology in the first place? (If the terms you were trying to use were generic to any application, they would have worked, but if they can only work with Microsoft Excel, then why are you not just telling Microsoft Excel directly instead of using a variable?) The usual answer to this question is "because Excel might have one of several different file names, and I need to figure out the right one." That's fine, but it means that you have a better option, which is to "tell" the application based on its bundle identifier, which is relatively constant compared to the name. For example:
tell application id "com.microsoft.Excel"
That way, it doesn't matter what the application is named ("Excel", "Microsoft Excel", "Excel 10.0", whatever); AppleScript will still find it.
--Chris Nebel
AppleScript Engineering
On May 17, 2011, at 7:42 AM, Leonid Bogdanov wrote:
> Zack,
>
> Thank you for your answer. I was aware of this trick.
> I thought that I just didn't know how to correctly write my script, that it had some syntactic problems.
>
> But it turned out that it is AppleScript pecularities.
>
>
> Tue, 17 May 2011 07:28:02 -0700 letter from Zack Jarrett <email@hidden>:
>
>> There's a very ugly way to do this, and it's fragile, but it will work if you
>> KNOW FOR SURE that the targeted application is on the system.
>>
>> What you do is build up your desired script as a string so that AppleScript
>> doesn't attempt to compile it at compile time. The string will only be
>> compiled at runtime (during the run script). Doing this is klunky, fragile,
>> and definitely ugly for longer scripts, but it can be an appropriate solution.
>>
>> Here's an example, using your scenario:
>>
>> set appName to "Microsoft Excel"
>> set s to "tell application " & quote & appName & quote & return & "return
>> display alerts" & return & "end tell"
>> set theResult to run script s
>>
>>
>> This will result in a runtime exception if Excel doesn't exist on the system
>> where this is running.
>>
>> Good luck!
>> Zack
>>
>> On May 17, 2011, at 6:07 AM, Leonid Bogdanov wrote:
>>
>>> Shane,
>>>
>>> Looks nice, but again, app name is hardcoded in one statement :)
>>> using terms from application "Microsoft Excel"
>>>
>>> and
>>> using terms from application appName
>>>
>>> doesn't work.
>>>
>>>
>>
>> _______________________________________________
>> Do not post admin requests to the list. They will be ignored.
>> AppleScript-Users mailing list (email@hidden)
>> Help/Unsubscribe/Update your Subscription:
>> Archives: http://lists.apple.com/archives/applescript-users
>>
>> This email sent to email@hidden
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> AppleScript-Users mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
> Archives: http://lists.apple.com/archives/applescript-users
>
> This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden