Tuve exactamente el mismo problema cuando quise poder lanzar ciertos enlaces en mi navegador no predeterminado, y las soluciones como LinCastor no funcionaban para mí. Esto es lo que hice (después de un TON de Google y horas de correr contra las paredes de ladrillo):
Crear servicio
Creé un Service
en Automator que recibe rich text
en any application
. Luego creé una serie de acciones (todas las cuales son Run AppleScript
) para transformar los datos seleccionados, poco a poco, en la URL deseada. Las siguientes secciones ilustran los pasos.
Para ser específicos, estos son todos los pasos dentro de un único flujo de trabajo, y todos están en el orden que se indica aquí (cada paso consume el resultado del paso anterior).
Editar : para las personas que no están familiarizadas con el uso de este servicio, puede hacer clic en cualquier enlace en cualquier aplicación (estaba usando enlaces en el Correo que eran texto enriquecido, lo que significa que el texto mostrado no estaba la URL), y busque el submenú Servicios en la ventana emergente. Elija su servicio de esa lista, y listo!
Obtenga los datos
on run {input}
-- Save off the old clipboard data and capture the current selection in its entirety
set oldClipboard to the clipboard as record
tell application "System Events" to keystroke "c" using command down
set plistData to ""
set retries to 50
-- Try to get the pList data from the clipboard (may take a little while to appear)
repeat while plistData = ""
set clipboardRecord to the clipboard as record
try
set plistData to «class weba» of clipboardRecord
-- In case you want to use RTF instead...
--set clipRTF to «class RTF » of clipboardRecord
on error msg
set retries to retries - 1
-- If we're out of retries then bail
if retries < 0 then
set the clipboard to oldClipboard
display dialog ("Failed to get the web data: " & msg) buttons {"OK"} default button 1
error number -1
end if
-- ...else ignore the error and retry after a small delay
delay 0.1
end try
end repeat
-- Restore the old clipboard data
set the clipboard to oldClipboard
-- Set up our intermediate plist file
set plistFileName to (path to temporary items as text) & "safarilink.plist"
set plistFRef to (open for access file plistFileName with write permission)
try
set eof plistFRef to 0
write plistData to plistFRef
close access plistFRef
--display dialog plistFileName
on error msg
display dialog ("PList write error: " & msg) buttons {"OK"} default button 1
close access plistFRef
error number -1
end try
-- Pass the pList file name to the next step
return plistFileName
end run
Extraer el enlace HTML
on run {input, parameters}
set plistFileName to (input as text)
-- Set up our intermediate HTML link file
set linkHtmlFileName to (path to temporary items as text) & "safarilink.html"
set linkHtmlFRef to (open for access file linkHtmlFileName with write permission)
try
tell application "System Events"
set plist to property list file plistFileName
set entry to contents of plist
--display dialog "Name: " & (name of entry)
--display dialog "Kind: " & (kind of entry)
--display dialog "Text: " & (text of entry)
set valueRec to (value of entry as record)
set webMainResource to webMainResource of valueRec
set webResourceData to webResourceData of webMainResource
--display dialog "Resourced"
set eof linkHtmlFRef to 0
write webResourceData to linkHtmlFRef
close access linkHtmlFRef
end tell
on error msg
display dialog ("Link HTML generation error: " & msg) buttons {"OK"} default button 1
close access linkHtmlFRef
error number -1
end try
-- Pass the HTML link file name to the next step
return linkHtmlFileName
end run
Cargue el HTML y extraiga la URL
on run {input, parameters}
set linkHtmlFileName to (input as text)
--set fileSize to 0
--tell application "Finder" to set fileSize to size of file linkHtmlFileName
try
set htmlContentParts to read file linkHtmlFileName using delimiter "="
on error msg
display dialog ("Link HTML load error: " & msg) buttons {"OK"} default button 1
close access htmlFRef
error number -1
end try
set hrefIndex to -1
repeat with index from 1 to count of htmlContentParts
if item index of htmlContentParts ends with "href" then
set hrefIndex to index
end if
end repeat
if hrefIndex = -1 then
display dialog "Selection does not contain a link!" buttons {"OK"} default button 1
error number -1
end if
set linkPart to item (hrefIndex + 1) of htmlContentParts
set splitParts to split(linkPart, "\"")
return item 2 of splitParts --index is base-1
end run
on split(theString, theDelimiter)
-- Save delimiters to restore old settings
set oldDelimiters to AppleScript's text item delimiters
-- Set delimiters to delimiter to be used
set AppleScript's text item delimiters to theDelimiter
-- Create the array
set theArray to every text item of theString
-- Restore the old setting
set AppleScript's text item delimiters to oldDelimiters
-- Return the result
return theArray
end split
En este punto, tienes tu URL (asegúrate de convertirla explícitamente en texto, para no terminar con misteriosos errores de AppleScript. Desde aquí, la usé para cargar automáticamente el enlace en Safari.
Usa tu URL
on run {input, parameters}
set linkURL to (input as text)
try
tell application "Safari"
if not (exists first window) then
make new window
set URL of last tab of first window to linkURL
set visible of first window to true
else
tell first window
set newTab to make new tab with properties {URL:linkURL}
set visible to true
set current tab to newTab
end tell
end if
activate
end tell
on error msg
display dialog ("Failed to load URL (" & linkURL & ") in Safari: " & msg) buttons {"OK"} default button 1
error number -1
end try
end run
Disfruta, ¡y espero que esto te ayude!