Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在提示用户使用AppleScript从列表中选择后在数组中查找位置_Javascript_Arrays_Arraylist_Applescript - Fatal编程技术网

Javascript 如何在提示用户使用AppleScript从列表中选择后在数组中查找位置

Javascript 如何在提示用户使用AppleScript从列表中选择后在数组中查找位置,javascript,arrays,arraylist,applescript,Javascript,Arrays,Arraylist,Applescript,我不能完全理解这一点,我刚刚开始编写脚本。为了解释,我想使用下面的代码允许用户从列表中选择一个项目。然后,我想将他们选择的内容转换为列表中的数字位置 例如,如果用户选择了iPod,我希望它将其输入保存为2,而不是iPod 以下是我所拥有的: set productList to {"iPad", "iPod", "iPhone", "Mac", "Apple TV", "Apple Watch", "Beats", "Apple Watch Edition", "Apple Watch Herm

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

例如,如果用户选择了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)

这对我使用最新版本的Sierra很有用

这将把“位置”存储在变量
属性itEmposition

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

你很接近。您只需添加一条语句:
将产品设置为产品的第1项

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
命令返回所选项目的列表。因此,在进行比较之前,必须从列表中获取项目


有关更多信息,请参见

,在发布此答案时,其他答案会返回列表中所选项目的位置,但它们不会考虑使用
从列表中选择
命令以编码方式进行的两种选择。如图所示,修复当前代码只需执行以下操作:

更改:

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

这样做的目的是处理用户按下取消按钮,或者将
yourProduct
的值设置为字符串,而不是列表,因为如果未按下取消,则从
选择列表
命令返回的值。然后您的
on-listPosition(这个项目,这个列表)
处理程序将在这个特定用例中正常工作

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

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