Re: Double Tell - Tell by Variable Question
Re: Double Tell - Tell by Variable Question
- Subject: Re: Double Tell - Tell by Variable Question
- From: Dave <email@hidden>
- Date: Fri, 26 Jun 2015 10:15:55 +0100
> On 26 Jun 2015, at 00:03, Shane Stanley <email@hidden> wrote:
>
> On 26 Jun 2015, at 12:54 am, Dave <email@hidden> wrote:
>>
>> I’m trying to avoid the use of “Calculator”
>
> You're not explaining *why* you don't want to use "Calculator". If it's because you're worried that the user might have changed the name of the app, it doesn't matter. But you can also use the bundle identifier:
>
> tell application id "com.apple.calculator"
Sorry, I don’t want to use the name “calculator”, because I want the Script to be able to talk to whichever app is currently being targeted. The selection of the Application has already been done in the Objective-C code.
For instance - a more real example:
on getFirstWindowTitle_(theApp)
tell application theApp —Could be file path, or process ID.
set myResult to the title of window 1
return myResult
end tell
end getFirstWindowTitle_
If I were to hardware “Calculator” it would only work for calculator and I’d lots of methods with one line difference - the tell statement.
Before this Script gets called, in Objective-C, the code has executed:
id<LTWAppleScriptObjCProtocol> myApp;
NSString* myAppID;
myAppID = @“com.looktowindward.calculator”;
myApp = [[NSClassFromString(@“LTWCaclulator") alloc] init];
[self.pDict setObject: myApp forKey: myAppID];
myAppID = @“com.looktowindward.bongodrums”;
myApp = [[NSClassFromString(@“LTWBongoDrums") alloc] init];
[self.pDict setObject: myApp forKey: myAppID];
myAppID = @“com.adobe.photoshop”;
myApp = [[NSClassFromString(@“LTWAdobePhotoshop") alloc] init];
[self.pDict setObject: myApp forKey: myAppID];
The above runs at initialization time. This is simplified, the real App will have to deal with multiple instances of a Target App, e.g. two different versions of Photoshop installed, in which case, instead of passing an ID, I’d pass a process ID or a absolute file path/URL of the target app.
When I call to run a method in the Script Handler Class, the code is something like:
-(void) doSomethingWithTargetApp:(NSString*) theAppID
{
id<LTWAppleScriptObjCProtocol> myApp;
NSString* myWindowTitle;
myApp = [self.pDict objectForKey:theAppID];
myWindowTitle = [myApp getFirstWindowTitle:theAppID];
}
Hardwiring “ com.looktowindward.bongodrums” is the same as using the name, it ties the Script Method to an App. If I could make it a variable, I'd be able to reuse the same Scripts for All Apps that I target. Otherwise, when I copy and rename the class to make a new Target Handler Class, I’d have edit each method to ensure that it’s “tell’ing” the correct App.
The other way of doing it that could work, would be to store the ID in a property of the Script which is set at initialization time. Then the target script would look like this:
on getFirstWindowTitle_(theApp)
tell application (the targetApp of me) —Could be file path, or process ID.
set myResult to the title of window 1
return myResult
end tell
end getFirstWindowTitle_
Maybe it’s impossible to do, I’m just trying to work out a viable template before I start duplicating the Script Handling Classes. I’d rather not have to edit the tell statement of each method of each App I wish to Target.
All the Best
Dave
_______________________________________________
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