-1

I'm using applescript to collect a lost of number from a website, and my script is working fine, I just get a lot of space and a line between the result which I can't figure out how to get rid of.

set numbersList to my getInputByClass2("sortable numPersonId", 1)


to getInputByClass2(theClass) 
    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 goodresult to items 2 thru -1 of numbersList as string

set theText to goodresult
set {tid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " "}
set temp to text items of theText
set AppleScript's text item delimiters to " " -- or use " " if you want to delete spaces
set theText to temp as text
set AppleScript's text item delimiters to tid

the result will be :

                                9990607901



                                8228210011



                                1555508116

when I expect to have: 9990607901 8228210011
1555508116

or 9990607901, 8228210011 , 1555508116

1

This isn't beautiful but it works-- you can probably clean it up. I commented things so you'll know what I was up to. Add this to the end of your existing script-- it start with the result you were already getting ("theText").

You have two problems to clean up: blank lines, and blanks (spaces) on either side of non-blank characters. The script below takes care of both.

set the_new_list to {}
set the_new_string to ""

-- starting with your current result-- "theText"

--the below was practice
--set theText to "    
--      9990607901
--
--
--
--                                8228210011     
--
--
--
--                                1555508116
--"

-- put the text into a list
set the_paragraphs to paragraphs of theText

-- loop through the items in the list
repeat with a_paragraph in the_paragraphs
    set the_new_string to ""
    set my_string to a_paragraph as string
    -- check for blank lines and skip/discard
    if my_string is "" then
        --do nothing
    else
        -- check for blanks surrounding non-blanks and remove
        repeat with a_character in (characters of my_string)
            if text of a_character = " " then
                --do nothing
            else
                set the_new_string to (the_new_string & text of a_character)
            end if
        end repeat
        -- now we have a clean, non-blank item for our new list
        if the_new_string is "" then
            --do nothing
        else
            set end of the_new_list to the_new_string
        end if
    end if
end repeat
--
set text_for_the_clipboard to ""
repeat with an_item in the_new_list
    set text_for_the_clipboard to text_for_the_clipboard & text of an_item & return
end repeat
set the clipboard to text_for_the_clipboard

Best of luck. This should do it for you.

  • that's great thank you, the only thing is I can't set the clipboard to the_new_list for some reason, but I have a better look in a minute – Kevin Aug 5 '16 at 8:12
  • 1
    I updated the script to include steps to set the clipboard. All you have to do is click where you want to paste, and paste. I had given you a list, which you can't paste. I converted the list that looked like this: {item 1, item 2, item 3} to a stack that looks like this: item 1 (return) item 2 (return) item 3 (return) etc. – Christian Boyce Aug 7 '16 at 1:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .