Browse Source

applescript

master
boB Rudis 4 months ago
parent
commit
f291a78c72
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
5 changed files with 114 additions and 0 deletions
  1. +1
    -0
      DESCRIPTION
  2. +3
    -0
      NAMESPACE
  3. +57
    -0
      R/applescript.R
  4. +1
    -0
      R/bitbar-package.R
  5. +52
    -0
      man/applescript.Rd

+ 1
- 0
DESCRIPTION View File

@@ -21,6 +21,7 @@ Imports:
usethis,
magrittr,
clipr,
tools,
httr,
sys,
miniUI,


+ 3
- 0
NAMESPACE View File

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

+ 57
- 0
R/applescript.R View File

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

}

+ 1
- 0
R/bitbar-package.R View File

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


+ 52
- 0
man/applescript.Rd View File

@@ -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)
}
}

Loading…
Cancel
Save