__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:
|
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.
__construct(array $options = array('error_on_empty' => true))
Initializes the wrapper object
array | $options | options that can be configured currently, only two options are available:
|
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.
string | $bundle | the bundle id of the workflow to trigger |
string | $trigger | the name of the trigger |
string|boolean | $argument | an argument to pass |
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:
ü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
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 |
an array of filtered items
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
.
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 |
the results
post(string $url, array|boolean $options = false, integer $cache_ttl = 600, string|boolean $cache_bin = true) : string
Makes a `POST` request
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 |
the results
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.
string|boolean | $bin | the cache bin to clear |
when encountering a sub-directory
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.
array | $options | the list of options to construct the notification |
success
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
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 |
the result of the user-input
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.
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. |
default returns nothing
log(string $message, string|integer $level = 'INFO', string $filename = 'workflow', boolean|integer $trace = false)
Writes a log message to a log file
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(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 ".
integer | $seconds | unix epoch time value |
boolean | $words | whether to use words or numerals |
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.
integer | $seconds | a unix epoch time |
a string that represents an approximate time
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"
array | $list | the array to add commas to |
boolean | $suffix | whether or not there is a suffix |
the array, but as a string with commas
create_request(string $url, array|boolean $options, integer $cache_ttl, string|boolean $cache_bin, string $type) : \Alphred\Request
Creates a request object
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 |
the request object