0 votos

Applescript está fallando inexplicablemente en Big Sur

EDIT: He hecho un script mucho más sencillo que presenta el mismo problema. El anterior script se mueve a continuación.

He estado usando TextExpander para ejecutar este script y recientemente comenzó a fallar después de actualizar a Big Sur. Pensando que estaba relacionado con TextExpander, he creado un acceso directo similar para ejecutarlo en BetterTouchTool pero estoy teniendo el mismo problema. No está lanzando un error visible, sólo reproduce el tono de error cuando termina y no produce nada. Lo extraño es que cuando lo ejecuto desde un editor de script, funciona bien. Es sólo cuando se llama desde otro proceso que falla.

La idea básica del script es tomar la entrada de números sin procesar en un cuadro de diálogo y dar salida a un código de tiempo formateado en la aplicación más cercana. Por ejemplo, al introducir "207" se obtiene "01:00:02:07".

He revisado el código y no puedo encontrar lo que podría estar causando el error ya que todo es una manipulación de texto muy básica. Cualquier idea sería de gran ayuda.

(El bloque de "Eventos del sistema" es para evitar que TextExpander se ponga al frente cuando aparezca el "Diálogo de visualización").

tell application "System Events"
 set frontProcessName to name of first process whose frontmost is true

 set test_text to text returned of (display dialog "Text?" default answer "")

tell application frontProcessName to activate
 end tell

 return "test_text"

Aquí está el original script:

tell application "System Events"
--Set process that called text expander aside so it can be returned to the front
set frontProcessName to name of item 1 of (processes whose frontmost is true)

-- Get the raw numbers from the user
set raw_timecode to text returned of (display dialog "Timecode" default answer "")

-- Set the default variables
set user_timecode to "" as string
set rt_length to the length of raw_timecode

--Check to see if the TC field is blank
if raw_timecode = "" then
    set raw_timecode to "01000000"
end if

--Parse the user supplied numbers and replace any "." with "00"
repeat with n from 1 to rt_length
    if character n of raw_timecode = "." then
        set user_timecode to user_timecode & "00" as text
    else
        set user_timecode to user_timecode & character n of raw_timecode as text
    end if

end repeat

--Set to 00:00:00:00 if only digit is 0 
if user_timecode = "0" then
    set base_timecode to "00000000"
else
    set base_timecode to "01000000"
end if

set x to the length of user_timecode

-- Trim extra digits off base timecode
if x = 8 and user_timecode ≠ "0" then
    set raw_timecode to user_timecode as string
else
    repeat while (length of base_timecode) + (length of user_timecode) > 8
        try
            set base_timecode to characters 1 thru -(x + 1) of base_timecode as string
            set raw_timecode to base_timecode & user_timecode as string
        on error
            display dialog "Invalid timecode"
            error number -128
        end try
    end repeat
end if
set new_timecode to characters 1 thru 2 of raw_timecode & ":" & characters 3 thru 4 of raw_timecode & ":" & characters 5 thru 6 of raw_timecode & ":" & characters 7 thru 8 of raw_timecode as text

end tell

-- Return the previous app that called text expander to the front
tell application frontProcessName to activate
delay 0.5

return new_timecode

0voto

wch1zpink Puntos 11

No tengo TextExpander ni BetterTouchTool por lo que no pude probar exactamente cómo necesitabas que se probara. Sin embargo, hice unos pequeños ajustes a tu código y guardé el script en la Barra de Menú script y lo ejecuté desde allí varias veces mientras trabajaba en varias aplicaciones diferentes. No obtuve ningún error por mi parte.

Si tu única razón para usar "System Events" es asegurarte de que la aplicación en la que estabas trabajando en ese momento, cuando ejecutas tu código, permanece al frente como máximo después de que tu ventana de diálogo desaparezca entonces todo lo que necesitas hacer es insertar un activate antes de su display dialog de la orden. Esto hará que la ventana de diálogo pase al frente y, cuando se desactive, la aplicación actual en la que estaba trabajando permanecerá al frente. Si es así, el uso de "Eventos del Sistema" no es necesario en absoluto.

-- Get the raw numbers from the user
activate
set raw_timecode to text returned of ¬
    (display dialog "Timecode" default answer "")

-- Set the default variables
set user_timecode to "" as string
set rt_length to the length of raw_timecode

--Check to see if the TC field is blank
if raw_timecode = "" then set raw_timecode to "01000000"

--Parse the user supplied numbers and replace any "." with "00"
repeat with n from 1 to rt_length
    if character n of raw_timecode = "." then
        set user_timecode to user_timecode & "00" as text
    else
        set user_timecode to user_timecode & character n of raw_timecode as text
    end if
end repeat

--Set to 00:00:00:00 if only digit is 0 
if user_timecode = "0" then
    set base_timecode to "00000000"
else
    set base_timecode to "01000000"
end if

set x to the length of user_timecode

-- Trim extra digits off base timecode
if x = 8 and user_timecode ≠ "0" then
    set raw_timecode to user_timecode as string
else
    repeat while (length of base_timecode) + (length of user_timecode) > 8
        try
            set base_timecode to characters 1 thru -(x + 1) of base_timecode as string
            set raw_timecode to base_timecode & user_timecode as string
        on error
            display dialog "Invalid timecode"
            error number -128
        end try
    end repeat
end if
set new_timecode to characters 1 thru 2 of raw_timecode & ":" & ¬
    characters 3 thru 4 of raw_timecode & ":" & characters 5 thru 6 of ¬
    raw_timecode & ":" & characters 7 thru 8 of raw_timecode as text

--set the clipboard to new_timecode
activate
display dialog new_timecode buttons {"OK"} giving up after 6
return new_timecode

enter image description here

AppleAyuda.com

AppleAyuda es una comunidad de usuarios de los productos de Apple en la que puedes resolver tus problemas y dudas.
Puedes consultar las preguntas de otros usuarios, hacer tus propias preguntas o resolver las de los demás.

Powered by:

X