Skip to content

Instantly share code, notes, and snippets.

@ltddev
Created March 19, 2014 22:00
Show Gist options
  • Save ltddev/9652354 to your computer and use it in GitHub Desktop.
Save ltddev/9652354 to your computer and use it in GitHub Desktop.
PythonistaConsoleTests.py
# PythonistaConsoleTests.py
# This script uses the custom console module and all exposed methods.
import console
# Clears all content from the console.
console.clear()
console.show_activity()
# Display a clickable web link which is opened in default browser or a specific one
# for atypical protocols.
link = 'http://www.ibm.com'
linkTitle = 'IBM Corp.'
console.set_color(1.0,0.0,0.0) #red
console.write_link(linkTitle,link)
console.set_color() # Set colour back to default
# Display a typical modal information/error dialog with message.
message = 'The script completed successfully.'
console.hud_alert(message, 'success') # default duration used, 1.8 seconds.
console.hud_alert(message, 'success', 3.0) # non-default duration used, 3 seconds.
message = 'The script encountered an unknown error'
console.hud_alert(message, 'error', 2.4) # 2.4 seconds.
# Displaying dialogs that get a single input value and returns that value to the caller. They vary
# in whether default is supplied and/or OK button text is overridden.
# simple ok-cancel style dialogs with console.alert(). That is, 2 button modal dialog.
# this dialog returns only the ok button value or nothing (if cancel pressed). Note that the
# integer value assigned to any button is based on the relative ordering of parameters.
# Dialog that gets a single input value and supplies a default value (optionally)
title='Favourite Ice Cream'
message='Please enter the name of your favourite ice cream'
defaultInput= 'Vanilla'
okButtonText='My OK!'
# Dislay a title and message and empty textfield for input entry with fixed Cancel and OK buttons.
# NO default value is provided. Return value is not which button but value of textfield.
iceCreamName = console.input_alert(title,message)
print('Ice cream name: ' + iceCreamName)
# Same as previous but supplies a defaul value
iceCreamName = console.input_alert(title,message,defaultInput)
print('Ice cream name: ' + iceCreamName)
# Same as previous but overides default OK button text with your value
iceCreamName = console.input_alert(title,message,defaultInput,okButtonText)
print('Ice cream name: ' + iceCreamName)
# Setting font type, font size and font colour in the console. This does not change fonts in dialogs
# only in the console
console.set_color(0.0,0.0,1.0) #(r,g,b)
console.set_font('Arial', 22)
print('Arial, 22')
console.set_font('Arial', 9)
print('Arial, 9')
console.set_color(1.0,0.0,0.0) #(r,g,b)
console.set_font('Courier', 7)
print('Courier, 7')
console.set_font('Arial', 12)
print('Arial, 12')
console.set_color(0.0,1.0,0.0) #(r,g,b)
console.set_font('Courier', 24)
print('Courier, 24')
# Set back to default, call with no args, set default colour
console.set_font()
console.set_color()
print('Default font and size and default colour reset')
# Show image in the console
imagePath = '/var/mobile/testimage.png'
console.show_image(imagePath)
console.hide_activity()
# Getting secure input (input is echoed back as dots as is typical native behaviour)
promptMessage = 'Enter your password and then press return'
secureInput = console.secure_input(promptMessage)
print('Secure input entered: ' + secureInput)
# A dialog with two choices - either cancel or custom action which could also just be OK.
title = 'Continue Running Script?'
message = 'To continue running this script click OK (or Show Me); to abort the script now click CANCEL'
okButtonText = 'OK'
customActionButton1 = 'Show Me'
returnVal = console.alert(title, message, okButtonText)
print('return value is:'+ str(returnVal))
returnVal = console.alert(title, message, customActionButton1)
print('return value is:'+ str(returnVal))
# Dialog with two buttons (plus the default cancel, so four button choice)
customActionButton2 = 'Custom Action'
returnVal = console.alert(title, message, customActionButton1, customActionButton2)
print('return value is:'+ str(returnVal))
# Dialog with the maximum three buttons (plus the default cancel, so four button choice)
returnVal = console.alert(title,message,customActionButton1, customActionButton2, okButtonText)
print('return value is:'+ str(returnVal))
#if returnVal==1:
# print ('OK button was selected')
console.hide_output()
# A dialog that only offers a cancel button. No other choice is possible. Since this causes
# termination of the script there is no point to getting the return value. For that reason
# it is last call in this script because it ignores anything coming after it. What follows
# is what could be a usecase for having only a cancel button.
warnMessage = 'This script is unlicensed and you must press cancel and abort it'
console.alert(warnMessage)
@ltddev
Copy link
Author

ltddev commented Mar 23, 2014

Just teaching myself about the Pythonista console module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment