首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >这是一种类型错配吗

这是一种类型错配吗
EN

Stack Overflow用户
提问于 2020-02-13 15:00:31
回答 1查看 77关注 0票数 0

我刚开始接触AppleScript,很难理解一些东西。为什么函数valid_hex不为这些项返回true?显然,通过读取段落和文本项目1和2,可以正确地从tsv获得数据项,因为输出字符串看起来很好。

是否存在不让valid_hex()完成其工作的类型不匹配?

代码语言:javascript
复制
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资产目录中,用作命名颜色。

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

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-13 15:13:10

问题就在眼前

代码语言:javascript
复制
repeat with c in (text items of s)

此时,text item delimiters设置为tab,因此只有一个文本项,它始终是整个字符串。

将每个字符替换为

代码语言:javascript
复制
repeat with c in (get characters of s)

get关键字对于只检索一次列表非常重要。

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

代码语言:javascript
复制
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

别忘了重置text item delimiters

代码语言:javascript
复制
set AppleScript's text item delimiters to atids

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

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60210745

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档