Applescript get the value as string and put the variable

2.5k Views Asked by At

I’m the beginner developer with AppleScript. I tried to write AppleScript for getting text value in multi rows and write to variable and and also it should be loop until end of data. After compile and run the AppleScript the result is correctly but after run agin without compile the result incorrect.

Here is the result after run 1st round:

{“3875.0", "2383.25", "missing value", "missing value", "missing value", "180.0", "300.0"}

After run 2nd round

{"3", "8", "7", "5", ".", "0", "", "", "2", "3", "8", "3", ".", "2", "5", "", "", "m", "i", "s", "s", "i", "n", "g", " ", "v", "a", "l", "u", "e", "", "", "m", "i", "s", "s", "i", "n", "g", " ", "v", "a", "l", "u", "e", "", "", "m", "i", "s", "s", "i", "n", "g", " ", "v", "a", "l", "u", "e", "", "", "1", "8", "0", ".", "0", "", "", "3", "0", "0", ".", "0"}

Explain about AppleScript

  1. Select range of value (row, column). If not select the script will show message.
  2. Repeat data from range select row and column until end of data. The result was string.
  3. Read the last position of data and remove “,”
  4. Set AppleScripts delimiter “,”
  5. Remove “,” and print to variable
  6. Crate new table with table’s property
  7. Set Header with value in row 1 and column A to H
  8. Received the variable and write to column G and loop until end of data.

Example source code

try
tell application "Numbers" to tell front document to tell active sheet
    set selected_table to first table whose class of selection range is range
    tell selected_table
        set my_selection to the selection range
        set begCol to address of first column of my_selection
        set endCol to address of last column of my_selection
        set begRow to address of first row of my_selection
        set endRow to address of last row of my_selection

        set delimiters to ","
        set getVal to ""
        repeat with j from begRow to endRow
            repeat with i from begCol to endCol
                set getVal to getVal & (value of cell j of column i of selected_table) & delimiters as string
                set getVal to getVal
            end repeat
        end repeat
    end tell
end tell
set getVal to (characters 1 thru -2 of getVal) as string
set AppleScript's text item delimiters to ","
set AmountVal to text items of getVal

tell application "Numbers"
    activate
    tell front sheet of front document
        set myOriginalTable to front table
        set setCols to 8
        set myNewTable to make new table with properties ¬
            {row count:(count of AmountVal) + 1, column count:setCols, header column count:0}
        tell myNewTable
            set value of cell 1 of column "A" to "cardNo"
            set value of cell 1 of column "B" to "emCode"
            set value of cell 1 of column "C" to "emName"
            set value of cell 1 of column "D" to "itemCode"
            set value of cell 1 of column "E" to "itemName"
            set value of cell 1 of column "F" to "effDate"
            set value of cell 1 of column "G" to "amt"
            set value of cell 1 of column "H" to "remark"

            set x to 2
            repeat with eachAmount in AmountVal
                set value of cell x of column "G" to eachAmount
                set x to (x + 1)
            end repeat
        end tell
    end tell
end tell

    display notification "Already Done!" with title "Numbers"
on error
    display dialog "Select a range first and then try again"
end try

Example of Data in Numbers

enter image description here

Expect result

enter image description here

0

There are 0 best solutions below