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

Applescript to cut and paste first word of Numbers cell

I'm creating a Service in Automator, and would like incorporate an Applescript that will "cut" the first word in any Numbers cell that I select, and "paste" it into the cell directly to the left of the the cell I've selected. Let's assume the column I'm selecting from is Column C. I'm no scripter and would appreciate any help.


As a very rough start, here is what I have (with some of things I'm trying to accomplish in brackets):



on run {input, parameters}

try

set MyText to input as string

set FirstWord to first word of MyText

set [remove the first word and a single space from what I've selected]

tell application "Numbers"

tell table 1 of sheet 1 of document 1


set theRow to [the row of the cell I've selected]

set value of cell 3 of theRow to FirstWord

end tell

end tell


end try

return input

end run





Thanks in advance!

Posted on May 19, 2012 9:59 PM

Reply
7 replies

May 20, 2012 6:09 AM in response to H L

Hi H L,

[…] an Applescript that will "cut" the first word in any Numbers cell that I select, and "paste" it into the cell directly to the left of the the cell I've selected.


The following script should do what you are asking for:


tell application "Numbers"

tell table 1 of sheet 1 of document 1

tell cell 1 of the selection range

set theRow to address of its row

set theColumn to address of its column

set theText to value

set value to ""

end tell

set value of cell (theColumn - 1) of row theRow to word 1 of theText

end tell

end tell


Maybe you might prefer to remove the line set value to "".

Message was edited by: Pierre L.

May 20, 2012 7:31 AM in response to Pierre L.

Thanks so much for your help.


This is very close to what I was after. It does place the first word in the cell directly to the left, but (after removing set theText to value) it leaves the original cell content the same as before. I was hoping to be able to also "cut" the first word + 1 space from the selected cell. (The selected cell will always have more than 1 word.)


On another issue I did not anticipate, when I click on a cell in Numbers, it does not automatically "highlight" the contents of the cell. Would it be possible to incorporate this ability into the script?


I appreciate your help.

May 20, 2012 9:20 AM in response to H L

Hi,


(table 1 of sheet 1) equal the first table of the first sheet at top left of the document (in the outline "Sheets") , so this is not always the active table , example if the selection is in the second table or the selection is in the another sheet.


Here's the solution :


In the top of your worflow (service) :

Select "no input" from the pop-up menu "Service receives"

Uncheck the checkBox "Replaces selected text"


Here is the script

------------------------------------------------

onrun {input, parameters}

tell application "Numbers"

settTablesto (tablesofsheetsoffrontdocumentwhoseitsselection rangeisnotmissing value)

repeatwithtintTables -- tables of each sheet

ifcontentsoftisnot {} then -- this list is not empty, it's the active table

set tCells to cells of selection range of (get item 1 of t) -- selection in active table

repeat with i in tCells -- each selected cell

set {tRow, n, tVal} to {row, address of column, value} of i

set value of cell (n - 1) of tRow to my cutFirstWord(tVal) -- set value of the left cell

end repeat

return

end if

end repeat

end tell

endrun


oncutFirstWord(t) -- get text after the first space

set tc to (count t)

repeatwithifrom 1 totc --** ignoring leading spaces **--

if text i of t is not " " then repeat with j from i to (tc - 1)

if text j of t is " " then return text (j + 1) thru -1 of t -- get text after the first space

end repeat

endrepeat

return "" -- one word or no character after the first space

endcutFirstWord

------------------------------------------------

This script works on one selected cell or (multiple selections but only if the selection is contiguous).

May 20, 2012 9:52 AM in response to H L

I was hoping to be able to also "cut" the first word + 1 space from the selected cell. (The selected cell will always have more than 1 word.)


Sorry. This new version of the script should do the trick:


tell application "Numbers"

tell table 1 of sheet 1 of document 1

tell cell 1 of the selection range

set theRow to address of its row

set theColumn to address of its column

set theText to value

set theFirstWord to word 1 of theText

set theNewText to text ((length of theFirstWord) + 2) through -1 of theText

set value to theNewText

end tell

set value of cell (theColumn - 1) of row theRow to theFirstWord

end tell

end tell

May 20, 2012 10:21 AM in response to Jacques Rioux

First, thanks so much for your help!


This is "close" -- this cuts the words after the first word and places those words in the cell directly to the left. But, I actually want to move the first word to the cell directly to the left, and keep the rest of the words in the original cell.


please see below:


_____________________________________________

|_______________|_apple cherry peach watermelon__|


to become this:

_________________________________________

|______apple_____|_ cherry peach watermelon__|



Thank you!

May 20, 2012 10:45 AM in response to Jacques Rioux

Ok, I have not paid enough attention, my first script pasted the (second word+) in the left cell and did not change the value of the selected cell.

Here is the modified version

--------------------------------------------------

onrun {input, parameters}

tell application "Numbers"

settTablesto (tablesofsheetsoffrontdocumentwhoseitsselection rangeisnotmissing value)

repeatwithtintTables -- tables of each sheet

ifcontentsoftisnot {} then -- the list is not empty, it's the selected sheet

set tCells to cells of selection range of (get item 1 of t) -- selection in active table

repeat with i in tCells -- each selected cell

set {tRow, n, tVal} to {row, address of column, value} of i

set {val2, val1} to my cutFirstWord(tVal)

set value of i to val1 -- set value of this selected cell

set value of cell (n - 1) of tRow to val2 -- set value of the left cell

end repeat

return

end if

end repeat

end tell

endrun


oncutFirstWord(t)

set tc to (count t)

repeatwithifrom 1 totc --** ignoring leading spaces **--

if text i of t is not " " then repeat with j from i to (tc - 1)

if text j of t is " " then return {text i thru (j - 1) of t, text (j + 1) thru -1 of t} --return list (first word, text after the first space)

end repeat

endrepeat

return {"", t} -- one word or no character after the first space

endcutFirstWord


You must do this to make it work :

In the top of your worflow (service) :

-- Select "no input" from the pop-up menu "Service receives"

-- Uncheck the checkBox "Replaces selected text"

Applescript to cut and paste first word of Numbers cell

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