2

This has to be fairly simple. I'm using Applescript with Pashua: Native macOS dialogs for scripting languages. What I'm trying to do is enter data into a text field in a dialog, and then copy that text field to the clipboard when the dialog box closes. Simply setting the clipboard to the variable tf doesn't work.

How can I get this script to copy the contents of tf to the clipboard? The Applescript results window shows {tf:"testme"} for an input of testme, but I don't want the output markup, only the text testme copied to the clipboard.

The relevant part of the script is at the bottom of the full script:

-- 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
2
  • do shell script “‘abcd’ | pbcopy”. See if you can get the variable in place of abcd string
    – anki
    Commented Mar 7, 2020 at 20:13
  • The shell script has no access to Pashua variables. Commented Mar 7, 2020 at 22:26

2 Answers 2

1

If you check Pashua's documentation, you will see that the result from showing the dialog is a record, with keys for the interface elements that you have declared. To get specific items from the dialog, just get the value for the desired key.

The getDialogConfiguration handler just configures the dialog - the showDialog handler is what actually shows the dialog and returns a result, so that is where you would work with whatever parts of the result you are interested in. Assuming you have the Pashua script and application in the same folder as the script, an example would be something like:

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
1
  • Thanks! That makes sense and it works. Where you added set the clipboard to tf of dialogResult is where the Pashua package example script output the data in a results dialog box, of which I deleted since I didn't think it was relevant. Commented Mar 9, 2020 at 3:02
0

Shows how you get data back from Pashua:

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

example applescript code to set clipboard data

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
1
  • Thanks, but that errors out with "The run handler is specified more than once" and "The variable tf is not defined." Commented Mar 7, 2020 at 22:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .