1

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

James Z
  • 12,209
  • 10
  • 24
  • 44
  • A really useful feature in *Script Editor* is the *Replies* window. At the bottom, you can show the *Result* pane by clicking on the ↵ button. If you click on the one next to it, you get other panes: *Result*, *Messages*, *Events*, *Replies*. Choose *Replies*, then run your script; every part of the script that generates a value will be output to the *Replies* window, and you can see where your script is doing something you don't want it to be doing. – CJK Mar 12 '18 at 06:52
  • @CJK Thanks in your suggestion. So, I would like to know to solve source code error – s.sasenharn Mar 15 '18 at 06:48
  • Did you use my suggestion at all to try and pinpoint where your error is occurring ? – CJK Mar 15 '18 at 07:41
  • CJK, I use already but I still confuse "why" alway need compile script before run again. Because, I thought source stable already. – s.sasenharn Mar 16 '18 at 01:38
  • Ok, I’m afraid I cannot help you. My apologies. Hopefully someone else will be able to. Good luck! – CJK Mar 16 '18 at 03:00
  • No problem actually your answer might be advantage with other beginner like me. – s.sasenharn Mar 16 '18 at 10:57

0 Answers0