Numbers AppleScript: paste clipboard as rows below

New to AppleScript in Numbers


How do I use AppleScript to paste the contents of the clipboard into an existing Numbers (3.6.1) document as additional rows, please?


The number of rows varies each time I need to do this; they're already tab-separated.


Thanks!

iMac with Retina 5K display, OS X El Capitan (10.11.2), Clean machine... no haxies; no Microsoft etc

Posted on Jan 26, 2016 1:13 PM

Reply
38 replies

Feb 4, 2016 1:14 PM in response to Mark Sealey

Hi Mark,


This works here:


set theDoc to POSIX path of (path to home folder as string) & "Sites/MySubfolder/MyDoc.numbers"

tell application "Numbers"


opentheDoc


-- etc.

end tell



As does this:


set theDoc to (path to home folder as string) & "Sites:MySubfolder:MyDoc.numbers"

--return theDoc

tell application "Numbers"


openfiletheDoc


-- etc.

end tell



Maybe a variation of one of these will work there.


SG

Feb 4, 2016 1:20 PM in response to SGIII

(Apologies for the (virtual) duplicate post… I got the dreaded red bar.)


Thanks. Unfortunately it's not that easy. I have tried a variety of things, believe me!


The two paths to the directory with the AS and the Numbers doc differ even after Sites, e.g.:

~/Sites/one_computer/work/spreadhseets/2016/names.numbers

vs:

~/Sites/another_computer/work/spreadhseets/ years/2016/names.numbers

hence the only simple solution - unless I have two statements conditional on some way of AS detecting the machine I'm on (maybe that is possible?) - seems to be to use the ./ convention for 'the current folder'. I looked at all the docs I could find to see how AS and POSIX handled that. No result so far.


Thanks!

Feb 4, 2016 2:03 PM in response to Mark Sealey

Since you say you are running the script from the target folder perhaps this will give you the path you want.


set pathToMe to path to me as string

set pathToMe to pathToMe's text 1 thru -16

tell application "Numbers" to open pathToMe & "MyDoc.numbers"



The -16 is chopping off the name of the script (in my case 'Untitled 2.scpt') so you are left with just the path, to which you then append the name of your Numbers document. Instead of hardwiring the relevant number in, you could of course get fancier and have the script detect the number of characters to chop off. But if your script is always named the same on each machine, then you might not need to do that.


SG

Feb 4, 2016 10:08 PM in response to Mark Sealey

To get the directory where current script is located, you may use this:



set cwd to do shell script "dirname " & (path to me)'s POSIX path's quoted form




Or this:



tell application "System Events" set cwd to (path to me)'s container's POSIX path end tell




Note that if script is not yet saved, it will return directory where current application (e.g., Script Editor) resides.


H

Feb 5, 2016 5:28 AM in response to Mark Sealey

Hi Mark,


Lots of ways to do this, it seems.


Here, this also works well, when run from Script Editor. (Unless you're working with shell scripts, not sure POSIX path has an advantage.)


tell application "System Events" to set myPath to (path to me)'s container's path

tell application "Numbers" to open myPath & "MyDoc.numbers"



SG

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Numbers AppleScript: paste clipboard as rows below

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.