Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript to paste the current date into the active cell in Numbers?

Hi all,

I'm a complete Applescript newbie trying to figure out Applescript. I want to have a shortcut to paste the current date in format DD/MM/YYYY into the active cell of a table but Im struggling to produce the script correctly. Any help would be greatly appreciated.

Many thanks,

Max

OS X Mavericks (10.9.2)

Posted on Apr 6, 2014 1:00 PM

Reply
Question marked as Best reply

Posted on Apr 6, 2014 1:36 PM

Hi Doc,


See this discussion: Re: How do I insert today's date in a cell on Numbers 3.0?


Regards,

Barry

5 replies

Apr 6, 2014 2:27 PM in response to DudleyDoc

Hi,


The first challenge is to format the date in AppleScript. The second is to get it into the selected cell, because if the format of the cell is the default Automatic, then (even if you type it in manually) your carefully formatted date (06/04/14) is converted to 6/4/14.


Here's one way:


set the_mon to month of (current date) as integer as string

if (count characters in the_mon) = 1 then set the_mon to "0" & the_mon

set the_day to day of (current date) as string

if (count characters in the_day) = 1 then set the_day to "0" & the_day

set the_date to the_day & "/" & the_mon & "/" & text 3 thru 4 of (year of (current date) as string)

tell application "Numbers"

activate

tell table 1 of sheet 1 of document 1

set format of cell 1 of selection range to text

set value of cell 1 of selection range to the_date

end tell

end tell


It's also possible to paste the information into the cell - see this thread for information on how to do it.

Apr 6, 2014 3:06 PM in response to HD

Many thanks to both Barry and HD. I also took advantage of the posted examples from http://macosxautomation.com/applescript/iwork/numbers/table-edit.htmlto give this:


set the_mon to month of (current date) as integer as string

if (count characters in the_mon) = 1 then set the_mon to "0" & the_mon

set the_day to day of (current date) as string

if (count characters in the_day) = 1 then set the_day to "0" & the_day

set the_date to the_day & "/" & the_mon & "/" & text 3 thru 4 of (year of (current date) as string)

tell application "Numbers"


activate


try

if not (exists document 1) then error number 1000

tell document 1

try

tell active sheet

set the selectedTable to ¬

(the first table whose class of selection range is range)

end tell

on error

error number 1001

end try

tell selectedTable

set format of cell 1 of selection range to text

set value of cell 1 of selection range to the_date

end tell

end tell

on error errorMessagenumbererrorNumber

if errorNumber is 1000 then

set alertString to "MISSING RESOURCE"

set errorMessage to "Please create or open a document before running this script."

else if errorNumber is 1001 then

set alertString to "SELECTION ERROR"

set errorMessage to "Please select a table before running this script."

else

set alertString to "EXECUTION ERROR"

end if


display alertalertStringmessageerrorMessagebuttons {"Cancel"}

error number -128

end try

end tell

Apr 6, 2014 6:50 PM in response to DudleyDoc

Here is a link to a Today Automator Service (Dropbox download) that will appear in you Services menu that will appear in your Services menu something like this:


User uploaded file


The script that it contains (which I've been using since Numbers 3 was released and it's been reliable) is somewhat simpler than those posted above!


on run

set date_ to ((current date) as string)

set the clipboard to the date_

tell application "Numbers"

activate

tell application "System Events"

keystroke "v" using {option down, shift down, command down}

end tell

end tell

end run


SG

Apr 7, 2014 1:46 AM in response to SGIII

Hi SGIII

I had seen that script whilst browsing this forum. However, I often have other data in the clipboard that I didnt want to lose. My problem now is getting the script to appear in the Numbers Services dropdown menu. Ive made the Service in Automator but it wont appear in the Numbers menu.

Max


Update: Double-clicked the workflow file to install it and solved the problem. Restarting Numbers may have helped as well.

Applescript to paste the current date into the active cell in Numbers?

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