Skip to content

Instantly share code, notes, and snippets.

@gniemetz
Created March 23, 2017 21:23
Show Gist options
  • Save gniemetz/90ffed86b6eb21e76d151c381d7941a2 to your computer and use it in GitHub Desktop.
Save gniemetz/90ffed86b6eb21e76d151c381d7941a2 to your computer and use it in GitHub Desktop.
Pashua Doubleclick Example
#!/usr/bin/env bash
PS4='+(${BASH_SOURCE:-}:${LINENO:-}): ${FUNCNAME[0]:+${FUNCNAME[0]:-}(): }'
# script path name
declare -r SCRIPT_PN="${0%/*}"
# script filename
declare -r SCRIPT_FN="${0##*/}"
declare -r APP_PN="${SCRIPT_PN/%\/${SCRIPT_FN}.app\/Contents\/MacOS/}"
declare -r APP_MACOS_PN="${APP_PN}/${SCRIPT_FN}.app/Contents/MacOS"
declare -r APP_RESOURCES_PN="${APP_PN}/${SCRIPT_FN}.app/Contents/Resources"
PashuaPath=""
PashuaConf=""
# late bound
PashuaChk2=""
PashuaTxtFld=""
PashuaObjB=""
PashuaPopMnu=""
PashuaRdBtn=""
PashuaCancelBtn=""
PashuaChk1=""
function locatePashua {
local _PashuaPath="${1}"
local BundlePath="Pashua.app/Contents/MacOS/Pashua"
PashuaPath=""
if [ -z "${1}" ]; then
SearchPaths[0]="${_PashuaPath}/${BundlePath}"
fi
SearchPaths[1]="${SCRIPT_PN}/Pashua"
SearchPaths[2]="${SCRIPT_PN}/${BundlePath}"
SearchPaths[3]="./${BundlePath}"
SearchPaths[4]="/Applications/${BundlePath}"
SearchPaths[5]="/Applications/Utilities/${BundlePath}"
SearchPaths[6]="${HOME}/Applications/${BundlePath}"
for SearchPath in "${SearchPaths[@]}"
do
if [[ -f "${SearchPath}" && -x "${SearchPath}" ]]; then
PashuaPath="${SearchPath}"
return 0
fi
done
return 1
}
# Function for communicating with Pashua
#
# Argument 1: Configuration string
# Argument 2: Path to a folder containing Pashua.app (optional)
function runPashua {
local _PashuaConfig="${1}"
local _PashuaPath="${2}"
local _PashuaConfigFile=""
local _PashuaResult=""
local _OldIFS _Line _Name _Value
locatePashua "${_PashuaPath}"
if [[ ${?} -ne 0 || -z "${PashuaPath}" ]]; then
echo "Error: Pashua could not be found" >&2
exit 1
fi
# Write config file
_PashuaConfigFile=$(/usr/bin/mktemp /tmp/pashua_XXXXXXXXX)
echo "${_PashuaConfig}" > "${_PashuaConfigFile}"
# Get result
_PashuaResult=$("${PashuaPath}" "${_PashuaConfigFile}")
# Remove config file
rm "${_PashuaConfigFile}"
_OldIFS="${IFS}"
IFS=$'\n'
# Parse result
for _Line in ${_PashuaResult}
do
_Name="$(echo "${_Line}" | sed 's/^\([^=]*\)=.*$/\1/')"
_Value="$(echo "${_Line}" | sed 's/^[^=]*=\(.*\)$/\1/')"
eval ${_Name}='${_Value}'
done
IFS="${_OldIFS}"
}
# Define what the dialog should be like
# Take a look at Pashua's Readme file for more info on the syntax
PashuaConf="
# Set window title
*.title = Welcome to Pashua
# Introductory text
PashuaTxt.type = text
PashuaTxt.default = Pashua is an application for generating dialog windows from programming languages which lack support for creating native GUIs on Mac OS X. Any information you enter in this example window will be returned to the calling script when you hit “OK”; if you decide to click “Cancel” or press “Esc” instead, no values will be returned.[return][return]This window shows nine of the UI element types that are available. You can find a full list of all GUI elements and their corresponding attributes in the documentation (➔ Help menu) that is included with Pashua.
PashuaTxt.height = 276
PashuaTxt.width = 310
PashuaTxt.x = 340
PashuaTxt.y = 44
PashuaTxt.tooltip = This is an element of type “text”
# Add a text field
PashuaTxtFld.type = textfield
PashuaTxtFld.label = Example textfield
PashuaTxtFld.default = Textfield content
PashuaTxtFld.width = 310
PashuaTxtFld.tooltip = This is an element of type “textfield”
# Add a filesystem browser
PashuaObjB.type = openbrowser
PashuaObjB.label = Example filesystem browser (textfield + open panel)
PashuaObjB.width=310
PashuaObjB.tooltip = This is an element of type “openbrowser”
# Define radiobuttons
PashuaRdBtn.type = radiobutton
PashuaRdBtn.label = Example radiobuttons
PashuaRdBtn.option = Radiobutton item #1
PashuaRdBtn.option = Radiobutton item #2
PashuaRdBtn.option = Radiobutton item #3
PashuaRdBtn.tooltip = This is an element of type “radiobutton”
# Add a popup menu
PashuaPopMnu.type = popup
PashuaPopMnu.label = Example popup menu
PashuaPopMnu.width = 310
PashuaPopMnu.option = Popup menu item #1
PashuaPopMnu.option = Popup menu item #2
PashuaPopMnu.option = Popup menu item #3
PashuaPopMnu.default = Popup menu item #2
PashuaPopMnu.tooltip = This is an element of type “popup”
# Add 2 checkboxes
PashuaChk1.rely = -18
PashuaChk1.type = checkbox
PashuaChk1.label = Pashua offers checkboxes, too
PashuaChk1.tooltip = This is an element of type “checkbox”
PashuaChk1.default = 1
PashuaChk2.type = checkbox
PashuaChk2.label = But this one is disabled
PashuaChk2.disabled = 1
PashuaChk2.tooltip = Another element of type “checkbox”
# Add a cancel button with default label
PashuaCancelBtn.type = cancelbutton
PashuaCancelBtn.tooltip = This is an element of type “cancelbutton”
PashuaDefltBtn.type = defaultbutton
PashuaDefltBtn.tooltip = This is an element of type “defaultbutton” (which is automatically added to each window, if not included in the configuration)
"
if [ -e "${APP_RESOURCES_PN}/dialog-bg.png" ]; then
PashuaConf="${PashuaConf}
img.type = image
img.x = 435
img.y = 248
img.maxwidth = 128
img.tooltip = This is an element of type “image”
img.path = ${APP_RESOURCES_PN}/dialog-bg.png"
fi
runPashua "${PashuaConf}"
echo "Pashua created the following variables:"
echo "PashuaTxtBox = '${PashuaTxtBox}'"
echo "PashuaTxtFld = '${PashuaTxtFld}'"
echo "PashuaObjB = '${PashuaObjB}'"
echo "PashuaPopMnu = '${PashuaPopMnu}'"
echo "PashuaRdBtn = '${PashuaRdBtn}'"
echo "PashuaCancelBtn = '${PashuaCancelBtn}'"
echo "PashuaChk1 = '${PashuaChk1}'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment