Applescript:从 shell 脚本中获取编号文件夹列表并确定最高值
Applescript: Getting a list of numbered folders from a shell script and identifying the highest value
我有两个不同的处理程序,一个获取文件夹中文件夹的名称(如“1.0”和“1.1”之类的数字),然后将其转换为 applescript 列表。然后它将该列表传递给另一个处理程序,该处理程序评估该列表并据称确定最高数字(我从 http://www.macosxautomation.com/applescript/sbrt/sbrt-03.html 获得)。
on set_values(project_path)
do shell script "ls " & project_path
get words of result
set allvalues to (result)
return allvalues
end set_values
然后我将结果转换为下一个处理程序的变量:
set values_list to result
这是从列表中获取最大数字的处理程序,由 macosxautomation 提供:
on highnum(values_list)
set the high_amount to ""
repeat with i from 1 to the count of the values_list
set this_item to item i of the values_list
set the item_class to the class of this_item
if the item_class is in {integer, real} then
if the high_amount is "" then
set the high_amount to this_item
else if this_item is greater than the high_amount then
set the high_amount to item i of the values_list
end if
else if the item_class is list then
set the high_value to highnum(this_item)
if the the high_value is greater than the high_amount then ¬
set the high_amount to the high_value
end if
end repeat
return high_amount
end highnum
问题是第二个处理程序只发出空响应,我不明白为什么。任何帮助表示赞赏。
该应用程序的重点是通过允许创建新的 'projects'、将现有应用程序导入新的 'project' 并允许编辑 'project' 轻松。如果您选择编辑项目,您可以选择 "minor update"(有效地将“0.0.1”添加到您的最新版本)或我已经完成的许多其他选项。我可以使用文本项定界符解决多位小数加法,但我不确定如何从 highnum() 处理程序中获取多位小数,这是过程的关键部分
highnum()
处理程序比较 real
和 integer
值,但 shell 脚本 returns string
值。
AppleScript 有一个用于比较数字字符串的内置选项。
编辑:考虑所有至少包含一个点且不包含任何字母的值。
on highnum(values_list)
set high_amount to "0.0"
considering numeric strings
repeat with aValue in values_list
set {TID, text item delimiters} to {text item delimiters, "."}
set aValueItems to text items of aValue
if (count aValueItems) > 1 then
set text item delimiters to ""
try
aValueItems as text as integer
if aValue > high_amount then set high_amount to contents of aValue
end try
end if
set text item delimiters to TID
end repeat
end considering
return high_amount
end highnum
这是 set_values()
处理程序的简单版本
on set_values(project_path)
return paragraphs of (do shell script "ls " & project_path)
end set_values
要添加您可以使用此处理程序的版本字符串,它会考虑具有不同 "decimal places".
的字符串
set newVersionString to addVersionStrings("1.1.1", "2.0") --> 3.1.1
on addVersionStrings(string1, string2)
set {TID, text item delimiters} to {text item delimiters, "."}
set textItems1 to text items of string1
set textItems2 to text items of string2
if (count textItems1) < (count textItems2) then
set minCount to count textItems1
set _sum to textItems2
set _add to textItems1
else
set minCount to count textItems2
set _sum to textItems1
set _add to textItems2
end if
repeat with i from 1 to minCount
set item i of _sum to (get (item i of _sum as integer) + (item i of _add as integer)) as text
end repeat
set theResult to _sum as text
set text item delimiters to TID
return theResult
end addVersionStrings
我有两个不同的处理程序,一个获取文件夹中文件夹的名称(如“1.0”和“1.1”之类的数字),然后将其转换为 applescript 列表。然后它将该列表传递给另一个处理程序,该处理程序评估该列表并据称确定最高数字(我从 http://www.macosxautomation.com/applescript/sbrt/sbrt-03.html 获得)。
on set_values(project_path)
do shell script "ls " & project_path
get words of result
set allvalues to (result)
return allvalues
end set_values
然后我将结果转换为下一个处理程序的变量:
set values_list to result
这是从列表中获取最大数字的处理程序,由 macosxautomation 提供:
on highnum(values_list)
set the high_amount to ""
repeat with i from 1 to the count of the values_list
set this_item to item i of the values_list
set the item_class to the class of this_item
if the item_class is in {integer, real} then
if the high_amount is "" then
set the high_amount to this_item
else if this_item is greater than the high_amount then
set the high_amount to item i of the values_list
end if
else if the item_class is list then
set the high_value to highnum(this_item)
if the the high_value is greater than the high_amount then ¬
set the high_amount to the high_value
end if
end repeat
return high_amount
end highnum
问题是第二个处理程序只发出空响应,我不明白为什么。任何帮助表示赞赏。
该应用程序的重点是通过允许创建新的 'projects'、将现有应用程序导入新的 'project' 并允许编辑 'project' 轻松。如果您选择编辑项目,您可以选择 "minor update"(有效地将“0.0.1”添加到您的最新版本)或我已经完成的许多其他选项。我可以使用文本项定界符解决多位小数加法,但我不确定如何从 highnum() 处理程序中获取多位小数,这是过程的关键部分
highnum()
处理程序比较 real
和 integer
值,但 shell 脚本 returns string
值。
AppleScript 有一个用于比较数字字符串的内置选项。
编辑:考虑所有至少包含一个点且不包含任何字母的值。
on highnum(values_list)
set high_amount to "0.0"
considering numeric strings
repeat with aValue in values_list
set {TID, text item delimiters} to {text item delimiters, "."}
set aValueItems to text items of aValue
if (count aValueItems) > 1 then
set text item delimiters to ""
try
aValueItems as text as integer
if aValue > high_amount then set high_amount to contents of aValue
end try
end if
set text item delimiters to TID
end repeat
end considering
return high_amount
end highnum
这是 set_values()
处理程序的简单版本
on set_values(project_path)
return paragraphs of (do shell script "ls " & project_path)
end set_values
要添加您可以使用此处理程序的版本字符串,它会考虑具有不同 "decimal places".
的字符串set newVersionString to addVersionStrings("1.1.1", "2.0") --> 3.1.1
on addVersionStrings(string1, string2)
set {TID, text item delimiters} to {text item delimiters, "."}
set textItems1 to text items of string1
set textItems2 to text items of string2
if (count textItems1) < (count textItems2) then
set minCount to count textItems1
set _sum to textItems2
set _add to textItems1
else
set minCount to count textItems2
set _sum to textItems1
set _add to textItems2
end if
repeat with i from 1 to minCount
set item i of _sum to (get (item i of _sum as integer) + (item i of _add as integer)) as text
end repeat
set theResult to _sum as text
set text item delimiters to TID
return theResult
end addVersionStrings