Can AppleScript be used for Hyperlink enabling?

I have a CSV file that I get from a third party that needs to have active hyperlinks. Presently, I open it in Numbers '09, then I need to double-click each cell with a hyperlink to make that link active/clickable, and finally I save the file in Numbers format.


I started looking at AppleScript and the Numbers dictionary, to see if there was a way to automate this, but I'm not finding anything suitable just yet. Can anyone offer creative workarounds or ways to accomplish this with Numbers and AppleScript (or some other way)?


Thanks in advance!

Posted on Aug 24, 2011 2:31 PM

Reply
14 replies

Aug 24, 2011 3:12 PM in response to joedandrea

I have an idea but for tests it would be useful to have a sample of the csv file (about ten rows)


You may send the file as a mail attachment.

Click my blue name to get my address.


Yvan KOENIG (VALLAURIS, France) jeudi 25 août 2011 00:11:47

iMac 21”5, i7, 2.8 GHz, 4 Gbytes, 1 Tbytes, mac OS X 10.6.8 and 10.7.0

My iDisk is : <http://public.me.com/koenigyvan>

Please : Search for questions similar to your own before submitting them to the community


To be the AW6 successor, iWork MUST integrate a TRUE DB, not a list organizer !

Aug 24, 2011 3:43 PM in response to KOENIG Yvan

Greetings!


Easy peasy. Just create a text file with a csv extension, and add ten lines to it, like this:


http://www.apple.com/

http://www.apple.com/

http://www.apple.com/

http://www.apple.com/

http://www.apple.com/

http://www.apple.com/

http://www.apple.com/

http://www.apple.com/

http://www.apple.com/

http://www.apple.com/


Save, then open this in Excel, and you should have a single column of ten rows, only they aren't hyperlink-enabled (yet).


I was trying something along the lines of:


tell application "Microsoft Excel"


activate


makenewhyperlink of column 8 atactive sheetwith properties {address:column 8}

end tell


(In my case it's column 8, but it can be any column.)


This does create hyperlinks, but the destination ends up being http://www.microsoft.com/mac/ instead! 🙂

Aug 25, 2011 3:10 AM in response to joedandrea

As you didn't sent a true file, I decided to treat the most general case which slows the execution.


--{code}

--[SCRIPT links_from_csv]

(*

Enregistrer le script en tant que Script : links_from_csv.scpt

déplacer le fichier ainsi créé dans le dossier

<VolumeDeDémarrage>:Users:<votreCompte>:Library:Scripts:Applications:Numbers:

Il vous faudra peut-être créer le dossier Numbers et peut-être même le dossier Applications.


Aller au menu Scripts , choisir Numbers puis choisir “links_from_csv”


On peut aussi enregistrer le script en tant qu’application

et double cliquer son icône ou glisser déposer une icône de CSV sur son icône.


--=====


L’aide du Finder explique:

L’Utilitaire AppleScript permet d’activer le Menu des scripts :

Ouvrez l’Utilitaire AppleScript situé dans le dossier Applications/AppleScript.

Cochez la case “Afficher le menu des scripts dans la barre de menus”.

Sous 10.6.x,

aller dans le panneau “Général” du dialogue Préférences de l’Éditeur Applescript

puis cocher la case “Afficher le menu des scripts dans la barre des menus”.


--=====


Save the script as a Script: links_from_csv.scpt


Move the newly created file into the folder:

<startup Volume>:Users:<yourAccount>:Library:Scripts:Applications:Numbers:

Maybe you would have to create the folder Numbers and even the folder Applications by yourself.


Go to the Scripts Menu, choose Numbers, then choose “links_from_csv”


We may also save it as an application,

double click it or drag and drop a CSV's icon on its icon.


--=====


The Finder’s Help explains:

To make the Script menu appear:

Open the AppleScript utility located in Applications/AppleScript.

Select the “Show Script Menu in menu bar” checkbox.

Under 10.6.x,

go to the General panel of AppleScript Editor’s Preferences dialog box

and check the “Show Script menu in menu bar” option.


--=====


Yvan KOENIG (VALLAURIS, France)

2011/08/25

*)

--=====


property le_fichier : ""


--=====


on run

set le_fichier to choose file with prompt "Choose a csv file" of type {"public.comma-separated-values-text"}


--set le_fichier to "Macintosh HD:Users:yvankoenig:Desktop:Sans titre.csv" as alias


run scriptmain

end run


--=====


script main

my activateGUIscripting()


tell application "Numbers"


openle_fichier

set le_fichier to ""

tell document 1 to tell sheet 1 to tell table 1


repeat with r from 1 to row count


set les_valeurs to (get value of cells of row r)

repeat with c from 1 to count of les_valeurs

if item c of les_valeurs contains "//www" then

set nom_cell to name of cell c of row r


clearcellnom_cell

set selection range to range nom_cell

repeat with un_carac in item c of les_valeurs

my raccourci("Numbers", un_carac, "")

end repeat -- with un_carac

end if -- item c

end repeat -- item c

end repeat -- with r

set selection range to range "A1"

end tell -- document

end tell -- Numbers

end script


--=====


on open sel

set le_chemin to sel's item 1

tell application "System Events" to tell disk item (le_chemin as text)

set maybe to (get type identifier is "public.comma-separated-values-text")

end tell

if maybe then

set le_fichier to le_chemin


run scriptmain

else

if my parle_anglais() then

display alert "" & le_chemin & return & "isn’t a CSV one !" buttons " OK " default button 1

else

display alert "" & le_chemin & return & "n’est pas un fichier CSV !" buttons " Vu " default button 1

end if

end if


end open


--=====


on parle_anglais()

return (do shell script "defaults read 'Apple Global Domain' AppleLocale") does not start with "fr_"

end parle_anglais


--=====


on activateGUIscripting()


(* to be sure than GUI scripting will be active *)

tell application "System Events"

if not (UI elements enabled) then set (UI elements enabled) to true

end tell

end activateGUIscripting


--=====

(*

==== Uses GUIscripting ====

*)

(*

This handler may be used to 'type' text, invisible characters if the third parameter is an empty string.

It may be used to 'type' keyboard raccourcis if the third parameter describe the required modifier keys.


I changed its name « shortcut » to « raccourci » to get rid of a name conflict in Smile.

*)

on raccourci(a, t, d)

local k

tell applicationa to activate

tell application "System Events" to tell application process a

set frontmost to true

try

t * 1

if d is "" then


key codet

else if d is "c" then


key codetusing {command down}

else if d is "a" then


key codetusing {option down}

else if d is "k" then


key codetusing {control down}

else if d is "s" then


key codetusing {shift down}

else if d is in {"ac", "ca"} then


key codetusing {command down, option down}

else if d is in {"as", "sa"} then


key codetusing {shift down, option down}

else if d is in {"sc", "cs"} then


key codetusing {command down, shift down}

else if d is in {"kc", "ck"} then


key codetusing {command down, control down}

else if d is in {"ks", "sk"} then


key codetusing {shift down, control down}

else if (d contains "c") and (d contains "s") and d contains "k" then


key codetusing {command down, shift down, control down}

else if (d contains "c") and (d contains "s") and d contains "a" then


key codetusing {command down, shift down, option down}

end if

on error

repeat with k in t

if d is "" then


keystroke (k as text)

else if d is "c" then


keystroke (k as text) using {command down}

else if d is "a" then


keystrokekusing {option down}

else if d is "k" then


keystroke (k as text) using {control down}

else if d is "s" then


keystrokekusing {shift down}

else if d is in {"ac", "ca"} then


keystroke (k as text) using {command down, option down}

else if d is in {"as", "sa"} then


keystroke (k as text) using {shift down, option down}

else if d is in {"sc", "cs"} then


keystroke (k as text) using {command down, shift down}

else if d is in {"kc", "ck"} then


keystroke (k as text) using {command down, control down}

else if d is in {"ks", "sk"} then


keystroke (k as text) using {shift down, control down}

else if (d contains "c") and (d contains "s") and d contains "k" then


keystroke (k as text) using {command down, shift down, control down}

else if (d contains "c") and (d contains "s") and d contains "a" then


keystroke (k as text) using {command down, shift down, option down}

end if

end repeat

end try

end tell

end raccourci


--=====

--[/SCRIPT]

--{code}


Yvan KOENIG (VALLAURIS, France) jeudi 25 août 2011 12:09:58

iMac 21”5, i7, 2.8 GHz, 4 Gbytes, 1 Tbytes, mac OS X 10.6.8 and 10.7.0

My iDisk is : <http://public.me.com/koenigyvan>

Please : Search for questions similar to your own before submitting them to the community


To be the AW6 successor, iWork MUST integrate a TRUE DB, not a list organizer !

Aug 25, 2011 3:41 AM in response to KOENIG Yvan

Here is an alternate, more efficient script.


--{code}

--[SCRIPT links_from_csv]

(*

Enregistrer le script en tant que Script : links_from_csv.scpt

déplacer le fichier ainsi créé dans le dossier

<VolumeDeDémarrage>:Users:<votreCompte>:Library:Scripts:Applications:Numbers:

Il vous faudra peut-être créer le dossier Numbers et peut-être même le dossier Applications.


Aller au menu Scripts , choisir Numbers puis choisir “links_from_csv”


On peut aussi enregistrer le script en tant qu’application

et double cliquer son icône ou glisser déposer une icône de CSV sur son icône.


--=====


L’aide du Finder explique:

L’Utilitaire AppleScript permet d’activer le Menu des scripts :

Ouvrez l’Utilitaire AppleScript situé dans le dossier Applications/AppleScript.

Cochez la case “Afficher le menu des scripts dans la barre de menus”.

Sous 10.6.x,

aller dans le panneau “Général” du dialogue Préférences de l’Éditeur Applescript

puis cocher la case “Afficher le menu des scripts dans la barre des menus”.


--=====


Save the script as a Script: links_from_csv.scpt


Move the newly created file into the folder:

<startup Volume>:Users:<yourAccount>:Library:Scripts:Applications:Numbers:

Maybe you would have to create the folder Numbers and even the folder Applications by yourself.


Go to the Scripts Menu, choose Numbers, then choose “links_from_csv”


We may also save it as an application,

double click it or drag and drop a CSV's icon on its icon.


--=====


The Finder’s Help explains:

To make the Script menu appear:

Open the AppleScript utility located in Applications/AppleScript.

Select the “Show Script Menu in menu bar” checkbox.

Under 10.6.x,

go to the General panel of AppleScript Editor’s Preferences dialog box

and check the “Show Script menu in menu bar” option.


--=====


Yvan KOENIG (VALLAURIS, France)

2011/08/25

*)

--=====


property le_fichier : ""

property les_valeurs : {}


--=====


on run

set le_fichier to choose file with prompt "Choose a csv file" of type {"public.comma-separated-values-text"}



run scriptmain

end run


--=====


script main


(*

Use properties le_fichier and les_valeurs

*)

local HYPERLINK_loc, r, c, un_lien

set HYPERLINK_loc to my getLocalizedTabularString("Numbers", "HYPERLINK") & "(" & quote

tell application "Numbers"


openle_fichier

set le_fichier to ""

tell document 1 to tell sheet 1 to tell table 1

repeat with r from 1 to row count

tell row r

set my les_valeurs to (get value of cells)

repeat with c from 1 to count of my les_valeurs

if item c of my les_valeurs contains "//www" then

set un_lien to item c of my les_valeurs

repeat while un_lien starts with space

set un_lien to text 2 thru -1 of un_lien

end repeat

repeat while un_lien ends with space

set un_lien to text 1 thru -2 of un_lien

end repeat

set value of cell c to "=" & HYPERLINK_loc & un_lien & quote & ")"

end if -- item c

end repeat -- item c

end tell -- row

end repeat -- with r

end tell -- document

end tell -- Numbers

set my les_valeurs to {}

end script


--=====


on open sel

set le_chemin to sel's item 1

tell application "System Events" to tell disk item (le_chemin as text)

set maybe to (get type identifier is "public.comma-separated-values-text")

end tell

if maybe then

set le_fichier to le_chemin


run scriptmain

else

if my parle_anglais() then

display alert "" & le_chemin & return & "isn’t a CSV one !" buttons " OK " default button 1

else

display alert "" & le_chemin & return & "n’est pas un fichier CSV !" buttons " Vu " default button 1

end if

end if


end open


--=====


on parle_anglais()

return (do shell script "defaults read 'Apple Global Domain' AppleLocale") does not start with "fr_"

end parle_anglais


--=====


on decoupe(t, d)

local oTIDs, l

set oTIDs to AppleScript's text item delimiters

set AppleScript's text item delimiters to d

set l to text items of t

set AppleScript's text item delimiters to oTIDs

return l

end decoupe


--=====

(*


Useful to get function’s localized name if we need to build formulas

examples:

set OFFSET_loc to my getLocalizedTabularString("Numbers", "OFFSET")

set ADDRESS_loc to my getLocalizedTabularString(theApp, "ADDRESS")

set INDIRECT_loc to my getLocalizedTabularString(theApp, "INDIRECT")


Requires :

decoupe()

get_iWorkNum()

getLocalizedName()

*)

on getLocalizedTabularString(theApp, x)

local path2app, p2bndl

tell applicationtheApp to activate


tell application "System Events"

set path2app to (application file of application processtheApp as text)


if exists folder (path2app & "Contents:Frameworks:") then

(*

Numbers from mac App Store *)

set p2bndl to path2app & "Contents:Frameworks:SFTabular.framework:Versions:A:Resources:"

else

(*

Numbers from iWork package *)

set p2bndl to (path to application support as text) & "iWork '" & my get_iWorkNum(theApp) & ":Frameworks:SFTabular.framework:Versions:A:Resources:"

end if

return my getLocalizedName(theApp, x, p2bndl)

end tell

end getLocalizedTabularString


--=====


on get_iWorkNum(a)

local verNum

tell application a to set verNum to item 1 of my decoupe(get version, ".")

if (a is "Numbers" and verNum is "2") or (a is "Pages" and verNum is "4") then

return "09"

else

return "11"

end if

end get_iWorkNum


--=====


on getLocalizedName(a, x, f)

tell application a to return localized string x from table "Localizable" in bundle file f

end getLocalizedName


--=====

--[/SCRIPT]

--{code}


I posted the two scripts because if the first one is slower, the created document is faster than the one created faster by the second one which emebed formulas.


CAUTION : I forgot to write that the first one requires that you turn on the feature,

choose Numbers > Preferences, click Auto-Correction,

and select “Automatically detect email and web addresses.”


Yvan KOENIG (VALLAURIS, France) jeudi 25 août 2011 12:35:28

iMac 21”5, i7, 2.8 GHz, 4 Gbytes, 1 Tbytes, mac OS X 10.6.8 and 10.7.0

My iDisk is : <http://public.me.com/koenigyvan>

Please : Search for questions similar to your own before submitting them to the community


To be the AW6 successor, iWork MUST integrate a TRUE DB, not a list organizer !

Aug 25, 2011 6:11 AM in response to KOENIG Yvan

Yvan,


No worries - I've send that file your way.


n.b.: "Automatically detect email and web addresses" is also enabled.


The only thing I haven't been able to do yet is get the Numbers script to show up in the script menu. Hiding/showing it doesn't help, but I do have ~/Library/Scripts/Applications/Numbers created in the meantime, and the script is in there. No errors.


Thanks again for the (extensive) help! This is also very instructive AppleScript - sincerely appreciated.


- Joe

Aug 25, 2011 6:42 AM in response to joedandrea

Run Applications:Utilities:Script Editor.app

menu Script Editor > Preferences

Check the box : Show Script Menu in menu bar

Check the box : Show computer's scripts (I translated from French title)

In the section Show application's scripts select one of the buttons Top or Bottom (I translated from French title)


Yvan KOENIG (VALLAURIS, France) jeudi 25 août 2011 15:41:52

iMac 21”5, i7, 2.8 GHz, 4 Gbytes, 1 Tbytes, mac OS X 10.6.8 and 10.7.0

My iDisk is : <http://public.me.com/koenigyvan>

Please : Search for questions similar to your own before submitting them to the community


To be the AW6 successor, iWork MUST integrate a TRUE DB, not a list organizer !

Aug 25, 2011 7:18 AM in response to joedandrea

Are you sure that Numbers was running when you triggered the Scripts menu ?


The beauty of its structure is that it displays only the scripts stored in the folder dedicated to the frontmost app.


Yvan KOENIG (VALLAURIS, France) jeudi 25 août 2011 16:17:49

iMac 21”5, i7, 2.8 GHz, 4 Gbytes, 1 Tbytes, mac OS X 10.6.8 and 10.7.0

My iDisk is : <http://public.me.com/koenigyvan>

Please : Search for questions similar to your own before submitting them to the community


To be the AW6 successor, iWork MUST integrate a TRUE DB, not a list organizer !

Aug 25, 2011 8:11 AM in response to joedandrea

It's because Excel is dumb.


When I open the exported xls file with the two schemes :


-- links "typed" by the script to be recognized as links

-- links encapsulated in the =HYPERLINK() formula


I get active links in the free clone entitled LibreOffice 😉


I can't test in the true Excel because I refuse to give a cent to Me…oSoft.


Yvan KOENIG (VALLAURIS, France) jeudi 25 août 2011 17:11:43

iMac 21”5, i7, 2.8 GHz, 4 Gbytes, 1 Tbytes, mac OS X 10.6.8 and 10.7.0

My iDisk is : <http://public.me.com/koenigyvan>

Please : Search for questions similar to your own before submitting them to the community


To be the AW6 successor, iWork MUST integrate a TRUE DB, not a list organizer !

Aug 25, 2011 8:38 AM in response to KOENIG Yvan

In an interesting twist, this only takes a few lines in Excel:Mac, using a macro:

Sub HyperlinkAdd()

For Each xCell In Selection

ActiveSheet.Hyperlinks.Add Anchor:=xCell, Address:=xCell.Formula

Next xCell

End Sub


Excel does support AppleScript though! Perhaps there's a way to do it that way, but now we're off-topic. I only post this here because I'm hopeful this may inspire a just-as-easy way to accomplish this in Numbers, which remains my fave!

Aug 25, 2011 8:45 AM in response to joedandrea

It's a shame but there is no macro in Numbers.

More, there is no way to link a script to a document.


Yvan KOENIG (VALLAURIS, France) jeudi 25 août 2011 17:44:09

iMac 21”5, i7, 2.8 GHz, 4 Gbytes, 1 Tbytes, mac OS X 10.6.8 and 10.7.0

My iDisk is : <http://public.me.com/koenigyvan>

Please : Search for questions similar to your own before submitting them to the community


To be the AW6 successor, iWork MUST integrate a TRUE DB, not a list organizer !

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.

Can AppleScript be used for Hyperlink enabling?

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