Save a user popup selection in a custom Automator Action

梦想与她 提交于 2019-12-13 15:31:39

问题


Using Xcode 5.* for a cocoa-applescript automator action. Interface is a a simple popup menu that gets populated using an outlet with:

tell thePopupMenu to removeAllItems()
tell thePopupMenu to addItemsWithTitles_(theList)

When the action is used in a workflow (a Service actually), I want the next time it is run and the action dialog shows up (I will have "Options:Show when run" selected), I want the popup menu to change the selection to the last one that was selected. Right now, the default first item shows, even though last time it was run, the user selected a different item in the popup menu. My thought was that I need to capture a change in the popup menu with a Sent Action handler, and then set some type of default. I have a working handler:

on thePopupMenuSentAction_(sender)
       set popupValue to (popupSelectedValue of my parameters()) as string
     -- save this selection somewhere???
end

What's the right way to save this? Do I use User Defaults? My Bindings are currenly all tied through Parameter object/controller. If I should use User Defaults, can someone give example code for setting up User Defaults, and then how to get and set a new value using Cocoa-Applescript?

If I can get the name string of the menu item saved somewhere, I can get the string and then change the selection of the popup menu in the

on opened {}
    -- set up the action interface
end

handler which gets called just before the action is displayed each time.

Thanks for any help, Joe


回答1:


I did mine a bit differently. I will assume you are referring to what XCode calls a "pop up button" (somewhat misleading). I did not use the parameters at all, although that is probably better for larger projects. Have a look at the code:

script Insert_Picture_into_Powerpoint_Slide_Show
    property parent : class "AMBundleAction"
    property menuChoices : {"Insert a Beginning", "Insert at End"}
    property menuSelection : missing value
    if (menuSelection as string) is "missing value"
        set menuSelection to 0 as integer -- sets default value
    end if
end script
  1. I bound Content Values to File's Owner and under Model Key Path I put menuChoices.

  2. Then you just bind the Selected Index to File's Owner and for the Model Key Path type menuSelection.

Defaults

On the initial run, if the user does not click anything, the menuSelection will be missing value. Because I couldn't find a way around this, I created a conditional which tests for this and sets it to the first choice by default (the one that is shown when you add the action).

When the user selections one of the menu choices, the choice is remembered on sequential runs.



来源:https://stackoverflow.com/questions/23920160/save-a-user-popup-selection-in-a-custom-automator-action

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