Alphred
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  • Download

Namespaces

  • Alphred
  • None

Classes

  • Alphred

Class 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.

Package: Alphred
Copyright: Shawn Patrick Rice 2014
License: MIT
Author: Shawn Patrick Rice rice@shawnrice.org
Located at Alphred.php

Methods summary

public
# __construct( array $options = [ 'error_on_empty' => true ] , array|boolean $plugins,… )

Initializes the wrapper object

Initializes the wrapper object

Parameters

$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)

$plugins,…
plugins to be run at load
private array
# parse_ini_file( )

Reads the workflow.ini file if it exists

Reads the workflow.ini file if it exists

Returns

array
an array of config values
public
# background( string $script, mixed $args = false )

Execute a php script in the background

Execute a php script in the background

Parameters

$script
path to php script
$args
args to pass to the script

Todo

Work on argument escaping
public boolean
# is_background( )

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

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

Returns

boolean
true if in the background; false if not

Since

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

Calls an Alfred External Trigger

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

$bundle
the bundle id of the workflow to trigger
$trigger
the name of the trigger
$argument
an argument to pass

Since

1.0.0

Uses

Alphred\Alfred::call_external_trigger()
public string
# theme_background( )

Tells you if the current theme is light or dark

Tells you if the current theme is light or dark

Returns

string
either 'light' or 'dark'

Uses

Alphred\Alfred::light_or_dark()
public array
# filter( array $haystack, string $needle, string|boolean $key = false, array $options = [] )

Filters an array based on a query

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: MATCH_ALLCHARS Combination of all other MATCH_* constants 127: MATCH_ALL

Parameters

$haystack
the array of items to filter
$needle
the search query to filter against
$key
the name of the key to filter on if array is associative
$options
a list of options to configure the filter

Returns

array
an array of filtered items
public
# add_result( array $item )

Adds a result to the script filter

Adds a result to the script filter

Parameters

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

Since

1.0.0
public mixed
# to_xml( )

Prints the script filter XML

Prints the script filter XML

Returns

mixed

Since

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

Makes a GET Request

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

$url
the URL
$options
an array of options for the request
$cache_ttl
cache time to live in seconds
$cache_bin
cache bin

Returns

string
the results

Uses

Alphred\Request
public string
# post( string $url, array|boolean $options = false, integer $cache_ttl = 600, string|boolean $cache_bin = true )

Makes a POST request

Makes a POST request

Parameters

$url
[description]
$options
an array of options for the request
$cache_ttl
cache time to live in seconds
$cache_bin
cache bin

Returns

string
the results

See

Alphred::get() See get() for details. The method is basically the same.

Uses

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

Creates a request object

Creates a request object

Parameters

$url
the URL
$options
an array of options for the request
$cache_ttl
cache time to live in seconds
$cache_bin
cache bin
$type
either get or post

Returns

Alphred\Request
the request object
public null
# clear_cache( string|boolean $bin = false )

Clears a cache bin

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

$bin
the cache bin to clear

Returns

null

Throws

Alphred\Exception
when encountering a sub-directory

Since

1.0.0

Uses

Alphred\Request::clear_cache()
public mixed
# config_read( string $key )

Reads a configuration value

Reads a configuration value

Parameters

$key
name of key

Returns

mixed
the value of the key or null if not set
public
# config_set( string $key, mixed $value )

Sets a configuration value

Sets a configuration value

Parameters

$key
the name of the key
$value
the value for the key
public
# config_delete( string $key )

Deletes a config value

Deletes a config value

Parameters

$key
name of the key
public boolean
# send_notification( array $options )

Sends a system notification

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

$options
the list of options to construct the notification

Returns

boolean
success

Since

1.0.0

See

Alphred\Notification::notify() For more information on how to call with the correct options.

Uses

Alphred\Notification::notify()

Todo

Check that return value is correct
public string|boolean
# get_password( string $account )

Gets a password from the keychain

Gets a password from the keychain

Parameters

$account
the name of the account (key) for the password

Returns

string|boolean
the password or false if not found

Uses

Alphred\Keychain::find_password()
public boolean
# delete_password( string $account )

Deletes a password from the keychain

Deletes a password from the keychain

Parameters

$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

Uses

Alphred\Keychain::delete_password()
public boolean
# save_password( string $account, string $password )

Saves a password to the keychain

Saves a password to the keychain

Parameters

$account
the name of the account (key) for the password
$password
the password

Returns

boolean

Uses

Alphred\Keychain::save_password()
public string
# get_password_dialog( string|boolean $text = false, string|boolean $title = false, string|boolean $icon = false )

Creates an AppleScript dialog to enter a password securely

Creates an AppleScript dialog to enter a password securely

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

Parameters

$text
the text for the dialog
$title
the title of the dialog; defaults to the workflow name
$icon
An icon to use with the dialog box

Returns

string
the result of the user-input

Uses

Alphred\Dialog
public mixed
# console( string $message, string|integer $level = 'INFO', integer|boolean $trace = false )

Sends a log message to the console

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

$message
the message to log
$level
the log level
$trace
how far to go in the stacktrace. Defaults to the last level.

Returns

mixed
default returns nothing

See

\Alphred\Log::console() More information on the console log

Uses

Alphred\Log
public
# log( string $message, string|integer $level = 'INFO', string $filename = 'workflow', boolean|integer $trace = false )

Writes a log message to a log file

Writes a log message to a log file

Parameters

$message
message to log
$level
log level
$filename
filename with no extension
$trace
how far back to trace

See

\Alphred\Log::file() More information on the file log

Uses

Alphred\Log
public string
# time_ago( integer $seconds, boolean $words = false )

Takes a unix epoch time and renders it as a 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

$seconds
unix epoch time value
$words
whether to use words or numerals

Returns

string
public string
# fuzzy_time_diff( integer $seconds )

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

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

$seconds
a unix epoch time

Returns

string
a string that represents an approximate time
public string
# add_commas( array $list, boolean $suffix = false )

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

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: <pre><span class="php-var">$list</span> = [ <span class="php-quote">'penny'</span> =&gt; <span class="php-quote">'one'</span>, <span class="php-quote">'quarters'</span> =&gt; <span class="php-quote">'three'</span>, <span class="php-quote">'dollars'</span> =&gt; <span class="php-quote">'five'</span> ];</pre> will render as: "one penny, three quarters, and five dollars"

Parameters

$list
the array to add commas to
$suffix
whether or not there is a suffix

Returns

string
the array, but as a string with commas
public
# activate( string $application )

Activates an application

Activates an application

Brings an application to the front, launching it if necessary

Parameters

$application
the name of the application
public array
# get_active_window( )

Gets the active window

Gets the active window

Returns

array
an array of [ 'app_name' => $name, 'window_name' => $name ]
public
# bring_to_front( string $application )

Brings an application to the front

Brings an application to the front

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

Parameters

$application
the name of an application

Magic methods summary

Alphred API documentation generated by ApiGen