Applescript 这是类型不匹配吗

Applescript 这是类型不匹配吗,applescript,Applescript,我是AppleScript的新手,理解某些东西有困难。为什么函数valid\u hex对于这些项不返回true?显然,通过阅读段落和文本项1和2,可以从tsv正确获取数据项,因为输出字符串看起来很好 是否存在不允许有效的_hex()执行其工作的类型不匹配 set inputStr to "8-1 Black\t232323\r\n8-2 Brown\tB5674D\r\n8-3 Orange\tFF7538\r\n8-4 Yellow\tFCE883\r\n8-5 Green\t1CAC78\r

我是AppleScript的新手,理解某些东西有困难。为什么函数valid\u hex对于这些项不返回true?显然,通过阅读段落和文本项1和2,可以从tsv正确获取数据项,因为输出字符串看起来很好

是否存在不允许有效的_hex()执行其工作的类型不匹配

set inputStr to "8-1 Black\t232323\r\n8-2 Brown\tB5674D\r\n8-3 Orange\tFF7538\r\n8-4 Yellow\tFCE883\r\n8-5 Green\t1CAC78\r\n8-6 Blue\t1F75FE\r\n8-7 Violet (Purple)\t926EAE\r\n8-8 Red\tEE204D"

set accepted to {}
set rejected to {}
set acceptedCount to 0
set rejectedCount to 0

set atids to AppleScript's text item delimiters
set AppleScript's text item delimiters to tab

repeat with p in (paragraphs of inputStr)
    set aLine to text of (p as string)

    repeat 1 times
        set colorName to text item 1 of aLine
        set hexColor to text item 2 of aLine

        log hexColor & " named " & colorName & " is valid: " & valid_hex(hexColor)

    end repeat

end repeat

on valid_hex(s)
    set validhex to {"#", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "a", "b", "c", "d", "e", "f"}
    if not (length of s = 6 or (length of s = 7 and s begins with "#")) then return false

    repeat with c in (text items of s)
        if validhex contains c then
            set status to true
        else
            set status to false
            exit repeat
        end if
    end repeat
    return status
end valid_hex
更新:根据已接受的答案,原始问题已得到解决。这是完整的脚本。它采用带名称的十六进制颜色的分隔列表,并为每个颜色创建一个Xcode.colorset文件夹,该文件夹可以直接拖动到Xcode资源目录中用作命名颜色

它工作得很好,但是如果创建了一个错误文件,它就不会进入同一个
工作文件夹

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

try
    set src to (choose file with prompt "choose input file")
    set o to (open for access src)
    set inputStr to (read o)
    close access o
end try

tell application "Finder"
    set working_path to container of (src) as string
end tell

set accepted to {}
set rejected to {}
set acceptedCount to 0
set rejectedCount to 0

repeat with aLine in (get paragraphs of inputStr)
    set old_delimits to AppleScript's text item delimiters -- Save the original delimiters
    set AppleScript's text item delimiters to tab
    set {colorName, hexColor} to text items of aLine
    set AppleScript's text item delimiters to old_delimits -- Restore the original delimiters

    repeat 1 times
        if not valid_hex_color(hexColor) then
            set rejectedCount to rejectedCount + 1
            copy "Rejected " & "\"" & colorName & "\"" & " with hex value: " & hexColor & "\n" to the end of rejected
            exit repeat
        else
            set acceptedCount to acceptedCount + 1
            set redComponent to text 1 thru 2 of hexColor
            set greenComponent to text 3 thru 4 of hexColor
            set blueComponent to text 5 thru 6 of hexColor
            set jsonString to "{\n\t\"info\": {\n\t\t\"version\": 1,\n\t\t\"author\": \"xcode\"\n\t},\n\t\"colors\": [\n\t\t{\n\t\t\t\"idiom\": \"universal\",\n\t\t\t\"color\": {\n\t\t\t\t\"color-space\": \"srgb\",\n\t\t\t\t\"components\": {\n\t\t\t\t\t\"red\": \"0x" & redComponent & "\",\n\t\t\t\t\t\"green\": \"0x" & greenComponent & "\",\n\t\t\t\t\t\"blue\": \"0x" & blueComponent & "\",\n\t\t\t\t\t\"alpha\": \"1.000\"\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t]\n}"


            tell application "Finder"

                set folderName to colorName & ".colorset"
                set fldr to (make new folder at working_path with properties {name:folderName})

            end tell

            set resultFilePath to (working_path as string) & folderName & ":Contents.json"
            set outFile to (open for access resultFilePath with write permission)
            write jsonString to outFile starting at 0
            close access outFile


            copy colorName & "\n" to the end of accepted

        end if
    end repeat
end repeat

if rejectedCount > 0 then
    set summary to "\nrejected " & rejectedCount & ":\n--------------------\n" & rejected & "\naccepted " & acceptedCount & ":\n--------------------\n" & accepted

    set errorFilePath to (working_path as string) & ":RejectedItems.txt"

    log working_path & errorFilePath

    set errorFile to (open for access errorFilePath with write permission)
    write summary to errorFile starting at 0
    close access errorFile

    display dialog ("Rejected " & rejectedCount & " items, see " & errorFilePath & ".")
end if

on valid_hex_color(s)
    set validhex to "0123456789ABCDEF"
    if s begins with "#" then set s to text 2 thru -1 of s
    if the length of s ≠ 6 then return false

    repeat with c in characters of s
        if validhex does not contain c then return false
    end repeat

    true
end valid_hex_color

问题就在眼前

repeat with c in (text items of s)
此时,
text item delimiters
被设置为
tab
,因此只有一个文本项始终是整个字符串

要获取每个字符,请将其替换为

repeat with c in (get characters of s)
get
关键字对于只检索一次列表很重要

第一个重复循环有点麻烦,这就足够了

repeat with aLine in (get paragraphs of inputStr)
    set {colorName, hexColor} to text items of aLine
    log hexColor & " named " & colorName & " is valid: " & valid_hex(hexColor)
end repeat
不要忘记重置
文本项分隔符

set AppleScript's text item delimiters to atids

检查字符串的更复杂方法是使用AppleScriptObjC和正则表达式(将
use
行放在脚本开头)


这很有效,谢谢你的建议。我把我的问题尽可能的简单化,但当我试图把它融入到我的整个项目中时,我遇到了另一个问题。我的数据的主要来源是一个正常使用的txt文件。但是当我添加
使用AppleScript版本“2.5”
使用框架“基础”
时,当我尝试
将src设置为(在提示“选择输入文件”的情况下选择文件)
时,我得到一个语法错误
预期“,”但找到了类名。
突出显示了“文件”。否则,如果我只使用原始形式的
valid\u hex
,一切都很好。您还必须添加行
use scripting additions
,因为
choose file
属于这些行。➀ “get关键字对于只检索一次列表很重要。”这是您最近确认为正确的,还是在早期版本的AppleScript中是正确的?➁ “永远不要忘记重置文本项分隔符”这不是必需的。与此相关的问题仅出现在创建单个AppleScript实例并在其中运行多个脚本的应用程序中。我不知道有多少人这样做。无论如何,一条更好的建议是,在使用
文本项拆分或将
列表强制为
文本之前,始终设置它们。那么就忘掉它们吧。@CJK这两个建议都是好的实践,而不是必要的。关于➀: 使用
get
关键字,列表只计算一次。特别是如果有一个
子句,例如b中的a的
的c为0,或者如果在枚举时修改了列表,那么这将更加有效。这会影响所有版本的AppleScript。如果没有
get
关键字,列表也只计算一次。由于
get
dereferences列表对象,它的效率实际上要低得多。在
的特殊情况下,其
过滤器直接用于
重复
循环的声明中,则需要
获取
,但并非出于您所述的原因:在枚举
之前,需要使用它来解除对其
过滤器的引用,否则对
项n
的引用将与对集合的引用复合,然后整个内容将被评估,充其量是不正确的,但更可能是由于错误的引用而失败。➀ <代码>重复1次
是不必要的,应该删除。➁
将aLine设置为text of(p作为字符串)
是对
text of…
的错误使用,
p
不需要强制设置为
string
。您可以安全地删除该行,然后在inputStr的段落中使用
repeat with aLine。➂ <代码>“#AABC#2”
应该无法通过十六进制验证,但它会通过(前提是您已经实现了瓦迪安的更正)。➃ 您的脚本的修改版本可在此处查看:。嗯,问题是,我的问题是,我的整个代码的最小化版本,以突出显示我遇到问题的部分。我很确定我需要“重复1次”,但我不是100%确定。我想在问题中添加我完整的更新代码以供参考,但我不想把任何人的时间浪费在我的初学者错误上。我想我可以把它贴出来作为参考答案,我只是不想让它变成另一个被否决的问题:)
use AppleScript version "2.5"
use framework "Foundation"

on valid_hex(s)
    set regex to current application's NSRegularExpression's regularExpressionWithPattern:"^#?[0-9A-Fa-f]{6}$" options:0 |error|:(missing value)
    return (regex's numberOfMatchesInString:s options:0 range:{location:0, |length|:(count s)}) as integer is 1
end valid_hex