substituer "automatiquement" http://foo.local par https://foo.bar.fr dans numbers

Je cherche un moyen de substituer "automatiquement" sans doute depuis un terminal les liens foo.local/... par ....dans l'ensemble des cellules d'un fichier .numbers sous imac osX, sachant que l'export en csv perd ces références et que, de même, ces références ne sont pas visibles à l'écran et ne sont accessibles que par l'option "modifier le lien" visible après un clic droit dans la cellule. J'exclus de le faire à la main sur plus d'une centaine de remplacements à effectuer.

Bien cordialement



[Edited by Moderator]

iMac 27″, macOS 10.13

Posted on May 4, 2024 3:42 AM

Reply
6 replies

May 5, 2024 12:59 PM in response to dheninfrompoissy

Not sure exactly what result you want but you can extract the url part of a hyperlink with this script.


-- select cells, command-c, run script, click cell 1x, command-v to paste urls

set the clipboard to (the clipboard as «class RTF »)
set theHTML to do shell script "pbpaste -Prefer rtf | textutil -convert html -stdin -stdout"
set htmlParts to splitText(theHTML, "<a href=\"http://")

set pasteStr to ""
repeat with i from 2 to (htmlParts's length)
	set pasteStr to pasteStr & htmlParts's item i's first word & return
end repeat
set the clipboard to pasteStr's text 1 thru -2
return pasteStr

to splitText(t, d)
	set text item delimiters to d
	t's text items
end splitText


  1. Copy-paste into Script Editor (in Applications > Utilities).
  2. Select the cells in Numbers with hyperlinks and then type command-c to copy to the system clipboard.
  3. Click the triangle run button in Script Editor
  4. Click a cell in Numbers and command-v to paste.


You should then see the urls pasted in a column.


Then you can use Numbers formulas to concatenate "https://" and ".fr" to produce the full url that you want, perhaps something like in the adjacent column.


"https://"&TEXTBEFORE(B2,".")&".bar.fr"


In a simple test I get something like this:




For my test the original link looked like this:



SG


May 6, 2024 11:47 AM in response to SGIII

Thank you for the clarification.


For my part, I am trying to read the url associated with the text of the cell in order to modify the url string, I think that will work. Really what I can't do is find the url, read it and then write the new string. Here is the start of the script


on replaceLinksInNumbers(filePath)

tell application "Numbers"

set theDocument to open filePath

tell theDocument

tell sheet 1

tell table 1

repeat with i from 2 to count of rows

set cellValue to value of cell ("B" & i)

if cellValue contains "mathilde.local" then

set newValue to my replaceText(cellValue, "http://foo.local/", "httpsr...")

set value of cell ("B" & i) to newValue

end if

-- This is where I'm trying to read the url of Bi,

-- for example B3 we see "Recapitualtion" (ie cellValue == "Recapitualtion" ) 

-- and if we right click we see "edit link"

-- This is what I want to do in applescript " (there are several hundred cells to modify) 

end repeat

end tell

end tell

save

close saving yes

end tell

end tell

end replaceLinksInNumbers

on replaceText(theText, searchString, replaceString)

set AppleScript's text item delimiters to searchString

set theTextItems to text items of theText

set AppleScript's text item delimiters to replaceString

set theText to theTextItems as string

set AppleScript's text item delimiters to ""

return theText

end replaceText


set filePath to "/Users/dhenin/Desktop/14_Travaux_en_cours/Chantier_Hermes/HermesTpe.numbers"

replaceLinksInNumbers(filePath)


"Be seeing you"


Number 6

SGIII wrote:

Not sure exactly what result you want but you can extract the url part of a hyperlink with this script.

-- select cells, command-c, run script, click cell 1x, command-v to paste urls

set the clipboard to (the clipboard as «class RTF »)
set theHTML to do shell script "pbpaste -Prefer rtf | textutil -convert html -stdin -stdout"
set htmlParts to splitText(theHTML, "<a href=\"http://")

set pasteStr to ""
repeat with i from 2 to (htmlParts's length)
set pasteStr to pasteStr & htmlParts's item i's first word & return
end repeat
set the clipboard to pasteStr's text 1 thru -2
return pasteStr

to splitText(t, d)
set text item delimiters to d
t's text items
end splitText

1.
Copy-paste into Script Editor (in Applications > Utilities).
2. Select the cells in Numbers with hyperlinks and then type command-c to copy to the system clipboard.
3. Click the triangle run button in Script Editor
4. Click a cell in Numbers and command-v to paste.

You should then see the urls pasted in a column.

Then you can use Numbers formulas to concatenate "https://" and ".fr" to produce the full url that you want, perhaps something like in the adjacent column.

"https://"&TEXTBEFORE(B2,".")&".bar.fr"

In a simple test I get something like this:


https://discussions.apple.com/content/attachment/654ca5b5-93a0-4319-9df2-d47bcb05a398


For my test the original link looked like this:


https://discussions.apple.com/content/attachment/ade143a4-7208-42c0-af4b-4deb36b7d2d4

SG



[Edited by Moderator]


May 7, 2024 8:35 AM in response to Badunit

Hello and thank you.

hello SGIII

Yes, I tried your utility and it works "comme un charme"? And I found elements there that were unknown to me. However your suggestion is to open numbers and select a set of cells and make a copy of them in the clipboard.

To continue my work I would prefer to use a script (.scpt), from command line. So I tried to use your instructions. and to limit, I hope, the difficulties, I am content to treat a single cell: B3 for which I am certain that a link to an http web page (mathilde.local) is present and modifiable by hand with a right click. However,


1. If I copy B3 into the clipbaord


set cellContent to value of cell "B3"
set the clipboard to cellContent
set the clipboard to (the clipboard as «class RTF »)
set theHTML to do shell script "pbpaste -Prefer rtf | textutil -convert html -stdin -stdout"
set htmlParts to splitText(theHTML, "<a href=\"http://")


I get the error :


:; osascript Test_B.scpt

Test_B.scpt:541:570: execution error: Error in Numbers: It is impossible to render data in the expected type. (-1700)


2. If I try not to use the clopboard:

set cellContent to value of cell "B3"
set cellContent to cellContent as string
set the clipboard to cellContent
set theHTML to do shell script "echo " & quoted form of cellContent & " | textutil -convert html -stdin -stdout"	
log theHTML

There is no compilation error but the reference is not retained and the script displays:

:; osascript "Test_string.scpt"

</html>s="p1">Summary</p>0px 0.0px; font: 12.0px 'Helvetica Light'}ml4/strict.dtd">



In any case, I am happy to find someone to talk to on this subject and I assure you that at 77 years old, I still have a lot to learn. I count on your indulgence.


Many thanks et be seing you.


May 8, 2024 10:02 AM in response to SGIII


clear





600 / 5 000



Résultats de traduction

Résultat de traduction


Hello.

Thank you for your proposal which I tested and appreciated. It works as you describe.

I found a workaround for my problem and I'll share it with you.


1. export file.numbers to name_bak.numers'09

2. rename name_bak.numbers'09 to name_bak.zip

3. open -a "Archive Utility" name_bak.zip

4. vim name_bak/index.xml

5. :%s/http:\/\/mathilde.local/https:\/\/mathilde.dhenin.fr/g

6 mv name_bak name_new

7 compress name_new

8 rename name_new.zip to name_new.numbers

9 open name_new.numbers


Everything is going as I wanted it (for now)


Thanks again !

Bonjour chez vous


substituer "automatiquement" http://foo.local par https://foo.bar.fr dans numbers

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