Tuesday, September 23, 2014

#1 2010-09-08 05:29:43 am

icaldoit
Member
Registered: 2010-09-01
Posts: 32

Automator Action - read/set Parameters

Hi,

I am new to AppleScriptObjC and have some problems switching over to it.

I wrote some Automator Actions in Applescript and I want to transfer them to 10.6.

But I couldn´t yet find out how to adress the paramters in the predefinded "Parameters"-Object of the action (getting/setting them).
I found a line of code that does this in Cocoa (p.e. for a boolean parameter), but I don´t now how to translate it to ASOC:

Applescript:

BOOL showurl = [[[self parameters] objectForKey:@"checkboxurl"] boolValue];

And how do I set the parameter´s value?

Thx!!!


iCalDoit - free suite of Automator actions for iCal

Filed under: Automator

Offline

 

#2 2010-09-08 06:42:35 am

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3636

Re: Automator Action - read/set Parameters

There are a couple of ways. You can get the parameters as an NSDictionary and call methods on it something like this:

Applescript:

set parametersDictionary to my parameters()
set showurl to parametersDictionary's valueForKey_("checkboxurl") as boolean

But I'd be inclined to coerce the properties dictionary to an AS record and try using it accordingly, something like this:

Applescript:

set parametersRecord to my parameters() as record
set showurl to checkboxurl of parametersRecord


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#3 2010-09-08 02:13:09 pm

icaldoit
Member
Registered: 2010-09-01
Posts: 32

Re: Automator Action - read/set Parameters

Thank you, that´s great!

It looks so simple, but these things are so hard to find out, if you "only" know AppleScript. With no docs or examles anywhere (TemperatureConverter isn´t an action).

I will provide a sample ASOC Action (Testaction) for beginners like me with code for the handling of gui and workflow input as soon as I know enough.

I´m still stuck at some points, like localized strings, the NSDatePicker's dateValue() and strange error logs.


iCalDoit - free suite of Automator actions for iCal

Offline

 

#4 2010-09-08 05:47:00 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3636

Re: Automator Action - read/set Parameters

Good luck! One of the great things about 10.6 is that Automator actions written in AS have access to AppleScriptObjC, which means much greater capabilities.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#5 2010-09-09 02:31:17 pm

icaldoit
Member
Registered: 2010-09-01
Posts: 32

Re: Automator Action - read/set Parameters

Maybe someone wants to check out the testaction I put on sourceforge to help me solve the open problems. I think it is pretty straight.


iCalDoit - free suite of Automator actions for iCal

Offline

 

#6 2010-09-09 05:16:24 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3636

Re: Automator Action - read/set Parameters

FYI, there are a couple of smple projects at <http://www.macosxautomation.com/automator/examples/ex04/index.html>.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#7 2012-04-30 04:28:08 am

icaldoit
Member
Registered: 2010-09-01
Posts: 32

Re: Automator Action - read/set Parameters

long time later...

reading the parameter's values works fine, but how do I set them?

I bound parameter values to interface elements like steppers and number formatted text fields. These parameter values get updated only when changing the values in the interface, not when changing them programmatically - like changing hours to "1" when minutes are set to "60" by the user.

Thus the displayed values are different from the actual parameters.

I already tried a lot like the following (the key "uiMinutes" is defined in AMDefaultParameters):

Applescript:

set ParametersDictionary to my parameters()
ParametersDictionary's setValue_forKey_(0, "uiMinutes")
log ParametersDictionary's valueForKey_("uiMinutes") -- returns: AMBundleAction

So I don´t even get the variable updated, updating the real vlaues of the paramaters fails as well:

Applescript:


my parameters's setparameters_(ParametersDictionary) -- fails


iCalDoit - free suite of Automator actions for iCal

Filed under: Parameters, Automator

Offline

 

#8 2012-04-30 04:36:08 am

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3636

Re: Automator Action - read/set Parameters

Off the top of my head, I'd try something like:


Applescript:

set ParametersDictionary to my parameters()
ParametersDictionary's setValue_forKey_(0, "uiMinutes")
set my parameters to ParametersDictionary


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#9 2012-04-30 05:28:46 am

icaldoit
Member
Registered: 2010-09-01
Posts: 32

Re: Automator Action - read/set Parameters

Hi Shane,

the last line,

Applescript:

set my parameters to ParametersDictionary

raises the error:

** INTERNAL ERROR: Uncaught Exception **
Exception: [<AMBundleAction 0x1002038e8> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key parameters.

It may be important to note that the code is part of an action handler, not the "on run"-part of the script. (I use a "Reset"-Button´s action handler for these experiments because here I need to set the parameters programmatically as well).

The parameter key "uiMinutes" does exist in Info.plist and I can read it without problems in the "on run"-script. In the action handler however, the value for "uiMinutes" returns "AMActionBundle".

The handler looks like this and is called by a button:

Applescript:

on resetTimes_(sender)
       TFMinutes's setIntegerValue_(0) -- resetting the interface elements,
       TFMinutes's setStringValue_("0") -- this does not refresh the bound parameters
       STMinutes's setIntegerValue_(0)
       TFHours's setIntegerValue_(0)
       TFHours's setStringValue_("0")
       STHours's setIntegerValue_(0)
       TFDays's setIntegerValue_(0)
       TFDays's setStringValue_("0")
       STDays's setIntegerValue_(0)
       TFMonths's setIntegerValue_(0)
       TFMonths's setStringValue_("0")
       STMonths's setIntegerValue_(0)
       TFYears's setIntegerValue_(0)
       TFYears's setStringValue_("0")
       STYears's setIntegerValue_(0)
       try
           set ParametersDictionary to my parameters()
           log "old uiMinutes = "
           log ParametersDictionary's valueForKey_("uiMinutes")
           ParametersDictionary's setvalue_forKey_(0, "uiMinutes")
           log "setValue_forKey_ executed"
           set my parameters to ParametersDictionary
           log "new uiMinutes = "
           log ParametersDictionary's valueForKey_("uiMinutes")
       on error
           log "error using dictionary"
       end try
   end resetTimes_


iCalDoit - free suite of Automator actions for iCal

Offline

 

#10 2012-04-30 05:58:02 am

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3636

Re: Automator Action - read/set Parameters

According to the sample, it's:

Applescript:

my parameters()'s setValue_forKey_(exportFieldNames, "exportFieldNames")

And makes sure you use "setValue", not "setvalue" as you posted.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#11 2012-04-30 06:35:59 am

icaldoit
Member
Registered: 2010-09-01
Posts: 32

Re: Automator Action - read/set Parameters

Xcode always compiles it to a lowercase "v", so I tried now:

my parameters()'s |setValue_forKey_|(0, "uiMinutes")

But when I run the action, the value of the key "uiMinutes" still is not zero, but the arbitrary value I had set beforehand with the stepper.

If it helps, you can find the project files here:
http://sourceforge.net/projects/icaldoi … p/download


iCalDoit - free suite of Automator actions for iCal

Offline

 

#12 2012-04-30 07:07:13 am

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3636

Re: Automator Action - read/set Parameters

icaldoit wrote:

Xcode always compiles it to a lowercase "v"

That's AppleScript's doing because you typed it that way first. You need to open the file outside Xcode, change the first such "v" without compiling, save, then try again.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#13 2012-04-30 07:24:46 am

icaldoit
Member
Registered: 2010-09-01
Posts: 32

Re: Automator Action - read/set Parameters

I replaced it in TextWrangler, now it´s spelled correctly.

But this does not solve the problem. It is like if there was a difference in runtime on accessing the parameters, depending on configuring or running an action.

Applescript:

my parameters()'s setValue_forKey_(59, "uiMinutes")

has no result in the handler, but works well in the "on run"-section of the script.


iCalDoit - free suite of Automator actions for iCal

Offline

 

#14 2012-04-30 07:52:10 am

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3636

Re: Automator Action - read/set Parameters

In your script you have:

my parameters()'s setValue_forKey_(0, "uiMinutes")
            log "new uiMinutes = "
            log ParametersDictionary's valueForKey_("uiMinutes")

At this stage, parameters() and ParametersDictionary are two completely separate things; ParametersDictionary is like a snapshot of the way parameters() was. Change the last line to:


Applescript:

           log parameters()'s valueForKey_("uiMinutes")


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#15 2012-04-30 08:03:07 am

icaldoit
Member
Registered: 2010-09-01
Posts: 32

Re: Automator Action - read/set Parameters

You´re right. While trying out different approaches, the code got somewhat inconsistent.

However, correcting it does not help. The log still results to "AMBundleAction" as before.

The expected result, in either case, should be a number, not "AMBundleAction" - a class (?).


iCalDoit - free suite of Automator actions for iCal

Offline

 

#16 2012-04-30 08:07:33 am

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3636

Re: Automator Action - read/set Parameters

What do you get logged if you also put that log line before the setValue_forKey_?


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#17 2012-04-30 08:27:36 am

icaldoit
Member
Registered: 2010-09-01
Posts: 32

Re: Automator Action - read/set Parameters

Applescript:

log parameters()'s valueForKey_("uiMinutes")
my parameters()'s setValue_forKey_(0, "uiMinutes")
log "new uiMinutes = "
log parameters()'s valueForKey_("uiMinutes")

Debugger Console:

2012-04-30 15:25:36.487 Automator[1416:a0f] AMBundleAction
2012-04-30 15:25:36.487 Automator[1416:a0f] new uiMinutes =
2012-04-30 15:25:36.487 Automator[1416:a0f] AMBundleAction


iCalDoit - free suite of Automator actions for iCal

Offline

 

#18 2012-04-30 07:37:43 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3636

Re: Automator Action - read/set Parameters

That suggests the problem happened before you got to trying to set the new values.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#19 2012-05-01 06:52:38 am

icaldoit
Member
Registered: 2010-09-01
Posts: 32

Re: Automator Action - read/set Parameters

So the problem happens before any of my code is being executed. What can I do about it?

It could be an issue with bindings though. I´ll try to describe it:

In main.xib, there´s

File's Owner, class: AMBundleAction, Outlets view->View, Referencing Bindings parameters-> Content Object Parameters
First Responder---
View---
Parameters, class: NSObjectController, Outlets none, Bindings: Content Object->File's Owner parameters, Referencing Bindings uiMinutes->Multiple (Value TextField, Value Stepper), Bindings: Content Object-> File's Owner parameters
Application---
"Move events by offset", class: move_events_by_offset,  Outlets: view->View, BTReset->RoundedTexturedButton, ..., Received Actions: resetTimes


iCalDoit - free suite of Automator actions for iCal

Offline

 

#20 2012-05-01 07:32:48 am

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3636

Re: Automator Action - read/set Parameters

It could be an issue with bindings though.

Then disconnect them and see if the problem goes away.

Sorry, but it's ages since I've looked at building Automator actions. They're a pain to test (as you know!), and I just don't have the time at present.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#21 2012-05-01 09:33:07 am

icaldoit
Member
Registered: 2010-09-01
Posts: 32

Re: Automator Action - read/set Parameters

I appreciate your help very much.And I think your feedback was very valuable. Without you, not knowing the syntax for certain, I wouldn´t have been able to track the problem down to bindings and connections.

Solution: I had to change File's Owners class to my action.


iCalDoit - free suite of Automator actions for iCal

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.110 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)