Applescript remove Missing value and unwanted data

applescriptjavascripttrim

I created a script to return some credit card informations from an internal website

                            <a href=\"/web/Support.aa/aa/g55erefesfsfsf/4.g.g.5.24.54\">
                                CC<br>Info
                            </a>

                    ", "

                            Visa (9999)

                    ", "

                            Visa (8888)

                    ", "

                            Visa (7777)

                    ", "

                            Visa (666)

                    ", "

                            Alipay

                    ", missing value, missing value, missing value}

I have two issue with my script,

  1. I can't get rid of the first link with is not a CC numbers
    tried :
    set myRawData to items 2 thru -1 of myRawData as string
    but don't seems to work.

  2. I don't manage to remove all the missing values.

  3. Can I remove some entry from the list that contain "none" ?

Here is my full script

tell application "Google Chrome"
    tell active tab of window 1 to set myRawData to execute javascript "var outPut=[]; var arr=document.getElementsByClassName('sortable fraudScoringTransactionCCSummary');for (var i in arr) {outPut.push(arr[i].innerHTML)};outPut;"
end tell

## set myRawData to items 2 thru -1 of myRawData as string -- not working
set myNewList to {}

repeat with each from 1 to count of items of myRawData
    set itemOnMyList to item each of myRawData
    if itemOnMyList is in myRawData and itemOnMyList is not in myNewList then set end of myNewList to itemOnMyList
end repeat

## not sur
##set myNewList to items 2 thru -1 of myNewList  -- not working

set countHowManyCC to count myNewList


return countHowManyCC

Best Answer

Using the returned output information posted in your OP while adding the missing opening curly brace and a opening double quote {" so as to have it compile as a list, the following example AppleScript code filters out the first item of the list as well as the items containing missing value in order to return a count of items that represent the data.

set myRawData to {"<a href=\"/web/Support.aa/aa/g55erefesfsfsf/4.g.g.5.24.54\">
                                CC<br>Info
                            </a>

                    ", "

                            Visa (9999)

                    ", "

                            Visa (8888)

                    ", "

                            Visa (7777)

                    ", "

                            Visa (666)

                    ", "

                            Alipay

                    ", missing value, missing value, missing value}


set myRawData to items 2 thru -1 of myRawData

set myNewList to {}
repeat with i from 1 to count myRawData
    if item i of myRawData does not contain missing value then
        copy item i of myRawData to end of myNewList
    end if
end repeat

return count myNewList

The following example AppleScript code adds a second filter to handle items containing "none":

set myRawData to items 2 thru -1 of myRawData

set myNewList to {}
repeat with i from 1 to count myRawData
    set thisItem to item i of myRawData
    if thisItem does not contain "none" then
        if thisItem is not missing value then
            copy item i of myRawData to end of myNewList
        end if
    end if
end repeat

return count myNewList

Note: The example AppleScript code is just that and does not employ any error handling and is meant only to show one of many ways to accomplish a task. The onus is always upon the User to add/use appropriate error handling as needed/wanted.

Related Question