How to get an Applescript to copy the contents of a variable to the clipboard

applescriptcopy/paste

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

Best Answer

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