2 votos

¿Cómo puedo conseguir que un Applescript copie el contenido de una variable al portapapeles?

Esto tiene que ser bastante sencillo. Estoy usando Applescript con Pashua: Diálogos nativos de MacOS para lenguajes de scripting . Lo que estoy tratando de hacer es introducir datos en un campo de texto en un cuadro de diálogo, y luego copiar ese campo de texto en el portapapeles cuando el cuadro de diálogo se cierra. Simplemente estableciendo el portapapeles a la variable tf no funciona.

Cómo puedo conseguir que este script copie el contenido de tf al portapapeles? La ventana de resultados de Applescript muestra {tf:"testme"} para una entrada de testme , pero no quiero la marca de salida, sólo el texto testme copiado en el portapapeles.

La parte relevante del script está en la parte inferior del script completo:

-- Get the path to the folder containing this script
tell application "Finder"
    set thisFolder to (container of (path to me)) as string
    if "Pashua.app:" exists then
        -- Looks like the Pashua disk image is mounted. Run from there.
        set customLocation to "Pashua:"
    else
        -- Search for Pashua in the standard locations
        set customLocation to ""
    end if
end tell

try
    set thePath to alias (thisFolder & "Pashua.scpt")
    set pashuaBinding to load script thePath

    tell pashuaBinding
        -- Display the dialog

        try
            set pashuaLocation to getPashuaPath(customLocation)
            set dialogConfiguration to my getDialogConfiguration(pashuaLocation)
            set theResult to showDialog(dialogConfiguration, customLocation)

        on error errorMessage
            display alert "An error occurred" message errorMessage as warning
        end try
    end tell

on error errStr number errorNumber
    display dialog errStr
end try

-- Returns the configuration string for an example dialog
on getDialogConfiguration(pashuaLocation)

    if pashuaLocation is not "" then

    end if

    return "

# Set window title
*.title = Page Settings

# Add a text field
tf.type = textfield
tf.label = Example textfield
tf.width = 310
"

    set the clipboard to tf   -- how do I set this variable to the clipboard?

end getDialogConfiguration

0 votos

do shell script “‘abcd’ | pbcopy” . A ver si puedes conseguir la variable en lugar de la cadena abcd

0 votos

El shell script no tiene acceso a las variables Pashua.

1voto

red_menace Puntos 111

Si comprueba Documentación de Pashua Verás que el resultado de mostrar el diálogo es un registro con las claves de los elementos de la interfaz que ha declarado. Para obtener elementos específicos del diálogo, basta con obtener el valor de la clave deseada.

El getDialogConfiguration sólo configura el diálogo - el showDialog es lo que realmente muestra el diálogo y devuelve un resultado, así que ahí es donde usted trabajaría con cualquier parte del resultado que le interese. Asumiendo que tienes el Pashua script y la aplicación en la misma carpeta que el script, un ejemplo sería algo así:

tell application "Finder" to set thisFolder to (container of (path to me)) as text -- get the folder path
try
    set thePath to alias (thisFolder & "Pashua.scpt")
    set pashuaBinding to load script thePath

    tell pashuaBinding
        try
            set dialogConfiguration to my getDialogConfiguration() -- configure the dialog
            set dialogResult to showDialog(dialogConfiguration, "") -- show the dialog
            set the clipboard to tf of dialogResult
        on error errmess number errnum
            display alert "Error " & errnum message "There was an error with the Pashua dialog:" & return & return & errmess
        end try
    end tell

on error errmess number errnum
    display alert "Error " & errnum message "There was an error setting up the Pashua script:" & return & return & errmess
end try

on getDialogConfiguration() -- return the configuration string
    return "
# Set window title
*.title = Page Settings

# Add a text field
tf.type = textfield
tf.label = Example textfield
tf.width = 310
"
end getDialogConfiguration

0 votos

Gracias. Eso tiene sentido y funciona. Donde has añadido set the clipboard to tf of dialogResult es donde el ejemplo del paquete Pashua script da salida a los datos en un cuadro de diálogo de resultados, del que he borrado ya que no me parecía relevante.

0voto

Kerry Puntos 100

Muestra cómo se recuperan los datos de Pashua:

https://github.com/BlueM/Pashua-Binding-AppleScript/blob/master/Example.applescript

ejemplo de código applescript para establecer los datos del portapapeles

on run

  set myData to "Hello, world"

  -- copy myData to the system clipboard
  set the clipboard to myData

  display dialog "The clipboard data is " & (the clipboard) giving up after 3

end run

0 votos

Gracias, pero eso da error con "El controlador de ejecución se especifica más de una vez" y "La variable tf no está definida".

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