【问题标题】:How to find position in array after prompting user to select from list using AppleScript提示用户使用AppleScript从列表中选择后如何在数组中查找位置
【发布时间】:2018-01-06 02:34:29
【问题描述】:

我不太清楚这一点,我才刚刚开始编写脚本。为了解释,我想使用下面的代码来允许用户从列表中选择一个项目。然后,我想将他们选择的内容转换为列表中的数字位置。

例如,如果用户选择了 iPod,我希望它将他们的输入保存为 2 而不是 iPod。

这是我所拥有的:

set productList to {"iPad", "iPod", "iPhone", "Mac", "Apple TV", "Apple Watch", "Beats", "Apple Watch Edition", "Apple Watch Hermes"}
set yourProduct to choose from list of productList with prompt "Select your product: "

on listPosition(this_item, this_list)
    repeat with i from 1 to the count of this_list
        if item i of this_list is this_item then return i
    end repeat
    return 0
end listPosition

set item_num to my listPosition(yourProduct, productList)

【问题讨论】:

    标签: javascript arrays arraylist applescript


    【解决方案1】:

    虽然其他答案在发布此答案时确实返回了列表中所选项目的位置,但它们没有考虑到可以使用choose from list 命令进行的两种选择 em> 以您编码的方式。如图所示,要修复当前的代码,您唯一需要做的就是:

    变化:

    set yourProduct to choose from list of productList with prompt "Select your product: "
    

    收件人:

    try
        set yourProduct to item 1 of (choose from list of productList with prompt "Select your product:")
    on error
        return
    end try
    

    它的作用是处理用户按下取消 按钮和/或将yourProduct设置为string,而不是 list,如果未按下 Cancel,则从 choose from list command 返回的内容。然后你的on listPosition(this_item, this_list) handler 将在这个特定的用例中正常工作。

    请注意,编写代码的方法通常不止一种,但通常最好使用 KISS 原则,尤其是在第一次学习如何编写代码时。

    【讨论】:

      【解决方案2】:

      这适用于我使用最新版本的 Sierra。

      这会将“位置”存储在变量property theItemPosition

      property productList : {"iPad", "iPod", "iPhone", "Mac", "Apple TV", "Apple Watch", "Beats", "Apple Watch Edition", "Apple Watch Hermes"}
      property theItemPosition : missing value
      
      set yourProduct to choose from list of productList with prompt "Select your product: "
      set yourProduct to yourProduct as string
      
      findPosition(productList, yourProduct)
      
      set theItemPosition to item 1 of result -- returns the chosen product to its position 
      
      on findPosition(theList, theListProductChosen)
          local theList, theListProductChosen, res
          try
              if theList's class is not list then error "not a list." number -1704
              if {theListProductChosen} is not in theList then return {}
              set res to {}
              script k
                  property l : theList
              end script
              repeat with i from 1 to count of k's l
                  if k's l's item i is theListProductChosen then set res's end to i
              end repeat
              return res
          on error eMsg number eNum
              error "Can't findPosition: " & eMsg number eNum
          end try
      end findPosition
      

      【讨论】:

        【解决方案3】:

        你很亲密。您只需添加一条语句:
        set yourProduct to item 1 of yourProduct

        set productList to {"iPad", "iPod", "iPhone", "Mac", "Apple TV", "Apple Watch", "Beats", "Apple Watch Edition", "Apple Watch Hermes"}
        set yourProduct to choose from list of productList with prompt "Select your product: "
        
        --- ADD This Line to Get Product from Returned List ---
        set yourProduct to item 1 of yourProduct
        
        on listPosition(this_item, this_list)
          repeat with i from 1 to the count of this_list
            if item i of this_list is this_item then return i
          end repeat
          return 0
        end listPosition
        
        set item_num to my listPosition(yourProduct, productList)
        

        choose from list 命令返回所选项目的列表。因此,在进行比较之前,您必须从列表中获取项目。

        欲了解更多信息,请参阅choose from list -- AppleScript Language Guide

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-08-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-05-19
          • 2021-12-21
          • 1970-01-01
          • 2019-02-09
          相关资源
          最近更新 更多