\Alphred

Wrapper Class.

This provides a simple wrapper for all of the important parts of the Alphred library. It also simplifies the usage of some of the internal components, so calls to this class do not always mirror calls to the internal components.

Summary

Methods
Properties
Constants
__construct()
background()
is_background()
call_external_trigger()
theme_background()
filter()
add_result()
to_xml()
get()
post()
clear_cache()
config_read()
config_set()
config_delete()
send_notification()
get_password()
delete_password()
save_password()
get_password_dialog()
console()
log()
time_ago()
fuzzy_time_diff()
add_commas()
activate()
get_active_window()
bring_to_front()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
parse_ini_file()
create_request()
No private properties found
N/A

Methods

__construct()

__construct(array  $options = array('error_on_empty' => true)) 

Initializes the wrapper object

Parameters

array $options

options that can be configured currently, only two options are available:

  1. error_on_empty - displays a script filter item when empty
  2. no_filter - initializes object without a script filter
  3. no_config - creates without a config item
  4. config_filename - sets filename for the config (default: config)
  5. config_handler - sets the handler for the config (default: ini)

background()

background(string  $script, mixed  $args = false) 

Execute a php script in the background

Parameters

string $script

path to php script

mixed $args

args to pass to the script

is_background()

is_background() : boolean

Tells you whether or not a script is running in the background

Returns

boolean —

true if in the background; false if not

call_external_trigger()

call_external_trigger(string  $bundle, string  $trigger, string|boolean  $argument = false) 

Calls an Alfred External Trigger

Single and double-quotes in the argument might break this method, so make sure that you escape them appropriately.

Parameters

string $bundle

the bundle id of the workflow to trigger

string $trigger

the name of the trigger

string|boolean $argument

an argument to pass

theme_background()

theme_background() : string

Tells you if the current theme is `light` or `dark`

Returns

string —

either 'light' or 'dark'

filter()

filter(array  $haystack, string  $needle, string|boolean  $key = false, array  $options = array()) : array

Filters an array based on a query

Passing an empty query ($needle) to this method will simply return the initial array. If you have fold on, then this will fail on characters that cannot be translitered into regular ASCII, so most Asian languages.

The options to be set are:

  • max_results -- the maximum number of results to return (default: false)
  • min_score -- the minimum score to return (0-100) (default: false)
  • return_score -- whether or not to return the score along with the results (default: false)
  • fold -- whether or not to fold diacritical marks, thus making über into uber. (default: true)
  • match_type -- the type of filters to run. (default: MATCH_ALL)

    The match_type is defined as constants, and so you can call them by the flags or by the integer value. Options: Match items that start with the query 1: MATCH_STARTSWITH Match items whose capital letters start with query 2: MATCH_CAPITALS Match items with a component "word" that matches query 4: MATCH_ATOM Match items whose initials (based on atoms) start with query 8: MATCH_INITIALS_STARTSWITH Match items whose initials (based on atoms) contain query 16: MATCH_INITIALS_CONTAIN Combination of MATCH_INITIALS_STARTSWITH and MATCH_INITIALS_CONTAIN 24: MATCH_INITIALS Match items if query is a substring 32: MATCH_SUBSTRING Match items if all characters in query appear in the item in order 64: MATCHALLCHARS Combination of all other ``MATCH*`` constants 127: MATCH_ALL

Parameters

array $haystack

the array of items to filter

string $needle

the search query to filter against

string|boolean $key

the name of the key to filter on if array is associative

array $options

a list of options to configure the filter

Returns

array —

an array of filtered items

add_result()

add_result(array  $item) 

Adds a result to the script filter

Parameters

array $item

an array of values to parse that construct an Alphred\Result object

to_xml()

to_xml() : mixed

Prints the script filter XML

Returns

mixed

get()

get(string  $url, array|boolean  $options = false, integer  $cache_ttl = 600, string|boolean  $cache_bin = true) : string

Makes a `GET` Request

This method is good for simple GET requests. By default, requests are cached for 600 seconds (ten minutes), and all options are passed via the $options array. Here are the options: params (array as $key => $value) auth (array as [ username, password ] ) user_agent (string) headers (array as list of headers to add)

Set only the options that you need.

To turn caching off, just set $cache_ttl to 0.

The $cache_bin is the subfolder within the workflow's cache folder. If set to true, then the cache bin will be named after the hostname of the URL. So, if you are requesting something from http://api.github.com/v3/shawnrice/repos, the cache bin would be api.github.com. If you were requesting http://www.google.com, then the cache bin would be www.google.com.

Parameters

string $url

the URL

array|boolean $options

an array of options for the request

integer $cache_ttl

cache time to live in seconds

string|boolean $cache_bin

cache bin

Returns

string —

the results

post()

post(string  $url, array|boolean  $options = false, integer  $cache_ttl = 600, string|boolean  $cache_bin = true) : string

Makes a `POST` request

Parameters

string $url

[description]

array|boolean $options

an array of options for the request

integer $cache_ttl

cache time to live in seconds

string|boolean $cache_bin

cache bin

Returns

string —

the results

clear_cache()

clear_cache(string|boolean  $bin = false) : null

Clears a cache bin

Clears a cache bin. If you send it with no argument (i.e.: $bin = false), then it will attempt to clear the workflow's cache directory. Note: this will throw an exception if it encounters a sub-directory. While it would be easy to make this function clear sub-directories, it shouldn't. If you are storing data other than responses in your cache directory, then use a cache-bin with the requests.

Parameters

string|boolean $bin

the cache bin to clear

Throws

\Alphred\Exception

when encountering a sub-directory

Returns

null

config_read()

config_read(string  $key) : mixed

Reads a configuration value

Parameters

string $key

name of key

Returns

mixed —

the value of the key or null if not set

config_set()

config_set(string  $key, mixed  $value) 

Sets a configuration value

Parameters

string $key

the name of the key

mixed $value

the value for the key

config_delete()

config_delete(string  $key) 

Deletes a config value

Parameters

string $key

name of the key

send_notification()

send_notification(array  $options) : boolean

Sends a system notification

Use this for async notifications or when running code in the background. If you want regular "end-of-workflow" notifications, then use Alfred's built-in set.

Since this uses AppleScript notifications, all of them will, unfortunately, have the icon for Script Editor in them, and this is not replaceable. If you want more control over your notifications, then use something like CocoaDialog or Terminal-Notifier.

Parameters

array $options

the list of options to construct the notification

Returns

boolean —

success

get_password()

get_password(string  $account) : string|boolean

Gets a password from the keychain

Parameters

string $account

the name of the account (key) for the password

Returns

string|boolean —

the password or false if not found

delete_password()

delete_password(string  $account) : boolean

Deletes a password from the keychain

Parameters

string $account

the name of the account (key) for the password

Returns

boolean —

true if it existed and was deleted, false if it didn't exist

save_password()

save_password(string  $account, string  $password) : boolean

Saves a password to the keychain

Parameters

string $account

the name of the account (key) for the password

string $password

the password

Returns

boolean

get_password_dialog()

get_password_dialog(string|boolean  $text = false, string|boolean  $title = false, string|boolean  $icon = false) : string

Creates an AppleScript dialog to enter a password securely

Note: this will return 'canceled' if the user presses the 'cancel' button

Parameters

string|boolean $text

the text for the dialog

string|boolean $title

the title of the dialog; defaults to the workflow name

string|boolean $icon

An icon to use with the dialog box

Returns

string —

the result of the user-input

console()

console(string  $message, string|integer  $level = 'INFO', integer|boolean  $trace = false) : mixed

Sends a log message to the console

If the log level is set higher than the level that this function is called with, then nothing will happen.

Parameters

string $message

the message to log

string|integer $level

the log level

integer|boolean $trace

how far to go in the stacktrace. Defaults to the last level.

Returns

mixed —

default returns nothing

log()

log(string  $message, string|integer  $level = 'INFO', string  $filename = 'workflow', boolean|integer  $trace = false) 

Writes a log message to a log file

Parameters

string $message

message to log

string|integer $level

log level

string $filename

filename with no extension

boolean|integer $trace

how far back to trace

time_ago()

time_ago(integer  $seconds, boolean  $words = false) : string

Takes a unix epoch time and renders it as a string

This also works for future times. If you set $words to true, then you will get "one" instead of "1". Past times are appended with "ago"; future times are prepended with "in ".

Parameters

integer $seconds

unix epoch time value

boolean $words

whether to use words or numerals

Returns

string

fuzzy_time_diff()

fuzzy_time_diff(integer  $seconds) : string

Takes a time and gives you a fuzzy description of when it is/was relative to now

So, something like "5 days, 16 hours, and 34 minutes ago" turns into "almost a week ago"; Something like "16 hours from now" turns into "yesterday"; and something like "1 month from now" turns into "in a month"; it's fuzzy. Also, the first strings need to be a unix epoch time, so the number of seconds since 1 Jan, 1970 12:00AM.

Parameters

integer $seconds

a unix epoch time

Returns

string —

a string that represents an approximate time

add_commas()

add_commas(array  $list, boolean  $suffix = false) : string

Implodes an array into a string with commas (uses an Oxford comma)

If you set $suffix to true, then the function expects an associative array as 'suffix' => 'word', so an array like:

$list = [ 'penny' => 'one', 'quarters' => 'three', 'dollars' => 'five' ];

will render as: "one penny, three quarters, and five dollars"

Parameters

array $list

the array to add commas to

boolean $suffix

whether or not there is a suffix

Returns

string —

the array, but as a string with commas

activate()

activate(string  $application) 

Activates an application

Brings an application to the front, launching it if necessary

Parameters

string $application

the name of the application

get_active_window()

get_active_window() : array

Gets the active window

Returns

array —

an array of [ 'app_name' => $name, 'window_name' => $name ]

bring_to_front()

bring_to_front(string  $application) 

Brings an application to the front

This is like activate, but it does not open the application if it is not already open.

Parameters

string $application

the name of an application

parse_ini_file()

parse_ini_file() : array

Reads the `workflow.ini` file if it exists

Returns

array —

an array of config values

create_request()

create_request(string  $url, array|boolean  $options, integer  $cache_ttl, string|boolean  $cache_bin, string  $type) : \Alphred\Request

Creates a request object

Parameters

string $url

the URL

array|boolean $options

an array of options for the request

integer $cache_ttl

cache time to live in seconds

string|boolean $cache_bin

cache bin

string $type

either get or post

Returns

\Alphred\Request

the request object