Benches Uncategorized

7

Apple Script - Copy the second row of a t…

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

Hi,

Hope you're well.

Just a quick question regarding some apple script terminology.

I've been trying to write an apple script which will copy the lowest line on the table "Company Info" (photos below) and paste it on the the lowest line of a table in another Numbers file.

I think I've managed to copy the whole "Company Info" table, but I only want to copy the lowest line (the one containing the numbers). I'd then like to paste it into "Table 1" which is in a numbers filed titled "Paste Here."

Is there a way to do this?

Thank you in advance.

7 replies

Sign in or join Beachball Help to reply.

  1. 0

    Roger Wilmut1

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    This is based on the script I gave you in your previous post - SGill may have a version of his, which is probably more elegant.

    The following assumes you start with both documents open, with 'Company Info' to the front.

    
    tell application "Numbers"
    	activate
    	tell document 1
    		tell sheet 1
    			tell table 1
    				set selection range to range "G1:G16"
    			end tell
    		end tell
    	end tell
    end tell
    
    copyit()
    
    
    tell application "System Events"
    	tell process "Numbers"
    		perform action "AXRaise" of window 2
    	end tell
    end tell
    
    
    tell application "Numbers"
    	activate
    	tell document 1
    		tell sheet 1
    			tell table 2
    				set selection range to range "C1"
    			end tell
    		end tell
    	end tell
    end tell
    
    pasteit()
    
    on copyit()
    	tell application "System Events"
    		tell application "Numbers" to activate
    		keystroke "c" using {command down}
    	end tell
    end copyit
    
    on pasteit()
    	tell application "System Events"
    		tell application "Numbers" to activate
    		keystroke "v" using {command down}
    	end tell
    end pasteit
    

    'AXRaise' brings the second document to the front. The script assumes that you know the row you want to copy in the Company Info document. If it is changing then selecting the bottom row involves counting the number of rows. I got as far as identifying the cell numbers at the beginning and end of the range:

    tell table 1
    				set rw to the row count as string
    				set clmn to the column count as string
    

    but this gives the number of the column across, not the letter and number which identifies it...

  2. 0

    Apple_Script

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Just out of interest, at the moment it keeps pasting the data into the header column of the table - is there a way to adjust it so that it just pastes into the selected row?

    I'm trying to identify the part of the code which changes the location of the paste - is it the "set selection range to range "C1"?

  3. 0

    Apple_Script

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Fantastic, Thank you

    Thank you very much for your help today gentlemen - There are still a couple of very minor changes I'd like to make to the automator tool, but you've been invaluable today. Thank you for your quick replies!

    All the best

  4. 0

    SGIII

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    copy the lowest line on the table "Company Info" (photos below) and paste it 
    on the the lowest line of a table in another Numbers file
    

    Another way to do that would be the script below. This assumes there are at least as many columns in the receiving table as in the source table. If not one might want some error trapping.

    SG

    -- replace names with actual names in your documents
    set srcDocNam to "MyDocName1.numbers"
    set srcShtNam to "Sheet 1"
    set srcTblNam to "Table 1"
    set tgtDocNam to "MyDocName2.numbers"
    set tgtShtNam to "Sheet 1"
    set tgtTblNam to "Table 1"
    
    tell application "Numbers"
    	tell document srcDocNam's sheet srcShtNam's table srcTblNam
    		set vv to last row's cells's value -- places last row's values in AppleScript list
    	end tell
    	tell document tgtDocNam's sheet tgtShtNam's table tgtTblNam
    		repeat with c from 1 to column count
    			set last row's cell c's value to vv's item c
    		end repeat
    	end tell
    end tell
    
    

  5. 0

    Apple_Script

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Perfecto, Thank you ever so much!

  6. 0

    Roger Wilmut1

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Good - evidently you know the addresses of the bottom row - just as well because the process of detecting them was getting messy.

  7. 0

    Roger Wilmut1

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    'C1' was fictitious, I was just showing what the script would look like. Judging by your image it should be A3.