diff --git a/DESCRIPTION b/DESCRIPTION index 3154ea2..df166c2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -21,6 +21,7 @@ Imports: usethis, magrittr, clipr, + tools, httr, sys, miniUI, diff --git a/NAMESPACE b/NAMESPACE index 0b39501..9b33ab6 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,6 +3,7 @@ export("%>%") export(add_entry) export(add_separator) +export(applescript) export(b64_image) export(bitbar_plugins_dir) export(new_bitbar_script) @@ -12,4 +13,6 @@ import(httr) import(miniUI) import(shiny) importFrom(magrittr,"%>%") +importFrom(tools,file_ext) +importFrom(tools,file_path_sans_ext) importFrom(usethis,use_template) diff --git a/R/applescript.R b/R/applescript.R new file mode 100644 index 0000000..dbd8813 --- /dev/null +++ b/R/applescript.R @@ -0,0 +1,57 @@ +#' Execute AppleScript and Return Results +#' +#' @md +#' @param script_source character vector of valid applescript source lines. +#' They will be turned into a single script file with a newline inserted +#' between each element of the vector. +#' @param extra_args the `applescript` binary takes arguments but we only +#' use the filename execution so you can place any other arguments +#' for it here. NOTE that there is a separate parameter for specifying +#' parameters +#' @param param a character vector of parameters to pass to the invokation. +#' @return anything the script returns (invisibly). +#' @note `stdout` is captured and returned but `stderr` (if any) is displayed in the console. +#' @export +#' @examples \dontrun{ +#' # open a folder +#' applescript( +#' sprintf( +#' 'tell app "Finder" to open POSIX file "%s"', +#' Sys.getenv("R_DOC_DIR") +#' ) +#' ) +#' +#' +#' # talk to an app and return data +#' res <- applescript(' +#' tell application "iTunes" +#' set r_name to name of current track +#' set r_artist to artist of current track +#' end +#' return "artist=" & r_artist & " || track=" & r_name +#' ') +#' +#' print(res) +#' } +applescript <- function(script_source, extra_args = c(), params = c()) { + + script_source <- paste0(script_source, collapse = "\n") + + tf <- tempfile(fileext = ".applescript") + on.exit(unlink(tf), add=TRUE) + + cat(script_source, file = tf) + + osascript <- Sys.which("osascript") + + args <- c(extra_args, tf, params) + + system2( + command = osascript, + args = args, + stdout = TRUE + ) -> res + + invisible(res) + +} \ No newline at end of file diff --git a/R/bitbar-package.R b/R/bitbar-package.R index 500ff54..f3e21aa 100644 --- a/R/bitbar-package.R +++ b/R/bitbar-package.R @@ -21,6 +21,7 @@ #' @md #' @name bitbar #' @importFrom usethis use_template +#' @importFrom tools file_path_sans_ext file_ext #' @import httr shiny miniUI #' @keywords internal #' @author Bob Rudis (bob@@rud.is) diff --git a/man/applescript.Rd b/man/applescript.Rd new file mode 100644 index 0000000..edb75c5 --- /dev/null +++ b/man/applescript.Rd @@ -0,0 +1,52 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/applescript.R +\name{applescript} +\alias{applescript} +\title{Execute AppleScript and Return Results} +\usage{ +applescript(script_source, extra_args = c(), params = c()) +} +\arguments{ +\item{script_source}{character vector of valid applescript source lines. +They will be turned into a single script file with a newline inserted +between each element of the vector.} + +\item{extra_args}{the \code{applescript} binary takes arguments but we only +use the filename execution so you can place any other arguments +for it here. NOTE that there is a separate parameter for specifying +parameters} + +\item{param}{a character vector of parameters to pass to the invokation.} +} +\value{ +anything the script returns (invisibly). +} +\description{ +Execute AppleScript and Return Results +} +\note{ +\code{stdout} is captured and returned but \code{stderr} (if any) is displayed in the console. +} +\examples{ +\dontrun{ +# open a folder +applescript( + sprintf( + 'tell app "Finder" to open POSIX file "\%s"', + Sys.getenv("R_DOC_DIR") + ) +) + + +# talk to an app and return data +res <- applescript(' +tell application "iTunes" + set r_name to name of current track + set r_artist to artist of current track +end +return "artist=" & r_artist & " || track=" & r_name +') + +print(res) +} +}