AppleScript摆脱了缺失的价值

时间:2016-05-18 10:42:40

标签: applescript missing-data

我在safari中从一行中提取了一些数字。

这是脚本的一小部分示例

set DATA39 to "0"
set DATA40 to "0"

to getInputByClass2(theClass, num) -- defines a function with two inputs, theClass and num
    tell application "Safari" --tells AS that we are going to use Safari
        set input to do JavaScript "
document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;" in document 1 -- uses JavaScript to set the variable input to the information we want
    end tell
    return input --tells the function to return the value of the variable input
end getInputByClass2



getInputByClass2("sortable DATAId", 1)
set theText to Unicode text
set DATA1 to getInputByClass2("sortable DATAId", 1)
###################################################################
getInputByClass2("sortable DATAId", 2)
set theText to Unicode text
set DATA to getInputByClass2("sortable DATA", 2)
################################################################### 

我不知道我收集了多少数据,所以现在,我将脚本限制为前30个结果

(data1,data2 data3)

唯一的问题是当小于30时,我有第一个结果,然后是“缺失值,缺失值,缺失值”,

我无法摆脱缺失值的文字?

我试图在脚本之前设置变量,如

将DATA1设置为缺失值 要么 将DATA1设置为“” 甚至 将DATA1设置为“0”

但没有运气,我知道我的脚本可能有意义,我确信有更好的方法来编写脚本,比如可能使用“重复?”

更新: 告诉应用程序“Safari”     做JavaScript“var outPut = []; var arr = document.getElementsByClassName('sortable DATAId'); for(var i in arr){outPut.push(arr [i] .innerHTML)}; outPut;”在文件1中          - > {“

            <a href=\"/WebObjects/MYSupport.woa/wo/999AAHW44DhfffC5gg7Vh0/9.0.9.7.1.95.1.9.1.9.1.9.5.9.1.3.1\">DSID</a>

    ", "

            998877557

    ", "

            997777557

    ", "

            998877557

    ", "

            998877657

    ", missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value}

结束告诉 告诉应用“脚本编辑器”     显示对话框{“

            <a href=\"/WebObjects/MYSupport.woa/wo/999AAHW44DhfffC5gg7Vh0/9.0.9.7.1.95.1.9.1.9.1.9.5.9.1.3.1\">DSID</a>

    ", "

            998877557

    ", "

            997777557

    ", "

            998877557

    ", "

            998877657

    ", missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value} buttons {"OK"} default button 1
    --> error number -1700 from {"


            <a href=\"/WebObjects/MYSupport.woa/wo/999AAHW44DhfffC5gg7Vh0/9.0.9.7.1.95.1.9.1.9.1.9.5.9.1.3.1\">DSID</a>

    ", "

            998877557

    ", "

            997777557

    ", "

            998877557

    ", "

            998877657

    ", missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value} to string

结果: 错误“无法生成{\”

            <a href=\\\"/WebObjects/MYSupport.woa/wo/999AAHW44DhfffC5gg7Vh0/9.0.9.7.1.95.1.9.1.9.1.9.5.9.1.3.1\\\">DSID</a>

    \", \"

            998877557

    \", \"

            997777557

    \", \"

            998877557

    \", \"

            998877657

    \", missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value} into type string." number -1700 from {"


            <a href=\"/WebObjects/MYSupport.woa/wo/999AAHW44DhfffC5gg7Vh0/9.0.9.7.1.95.1.9.1.9.1.9.5.9.1.3.1\">DSID</a>

    ", "

            998877557

    ", "

            997777557

    ", "

            998877557

    ", "

            998877657

    ", missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value} to string

1 个答案:

答案 0 :(得分:1)

只需将所有元素放入数组中,使用javascript循环获取每个元素的innerHtml

输出将是一个列表,使用此列表来获取项目而不是具有超过30个变量。

set allData to my getInputByClass2("sortable DATAId")
-- to get the tenth value, use this --> item 10 of allData 

to getInputByClass2(theClass) -- this return a list of values
    tell application "Safari"
        set r to do JavaScript "var outPut=[]; var arr=document.getElementsByClassName('" & theClass & "');for (var i in arr) {outPut.push(arr[i].innerHTML)};outPut;" in document 1
    end tell
    return strings of r --- this remove all 'Missing Value' from a list of strings
end getInputByClass2

如果要限制列表的长度,请使用此

set allData to my getInputByClass2(theClass, 30) -- 30 values maximum
-- to get the tenth value in the list, use this --> item 10 of allData 

to getInputByClass2(theClass, num) --this return a list of values
    tell application "Safari"
        set r to do JavaScript "var n=" & num & "; var outPut=[]; var arr=document.getElementsByClassName('" & theClass & "');var tLen=arr.length; if (n>tLen) {n=tLen;}; for (var i = 0; i < n; i++) {outPut.push(arr[i].innerHTML)}; outPut;" in document 1
    end tell
    return strings of r --- this remove all 'Missing Value' from a list of strings
end getInputByClass2

更新missing value来自没有innerHTML的元素。

要从字符串列表中删除missing value,请使用此语法(set myList to strings of someList)。

我更新了脚本。