Browse Source

applescript

master
boB Rudis 6 years ago
parent
commit
731af4f112
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 2
      NAMESPACE
  2. 3
      NEWS.md
  3. 57
      R/applescript.R
  4. 13
      R/logger.R
  5. 1
      README.Rmd
  6. 1
      README.md
  7. BIN
      inst/modules/__pycache__/dsstore.cpython-37.pyc
  8. 52
      man/applescript.Rd
  9. 16
      man/logger.Rd

2
NAMESPACE

@ -1,8 +1,10 @@
# Generated by roxygen2: do not edit by hand
export(airport_scan)
export(applescript)
export(find_dsstore)
export(kernel_state)
export(logger)
export(read_dsstore)
export(read_plist)
export(software_update_history)

3
NEWS.md

@ -1,2 +1,5 @@
0.1.0 - WIP
* Added `applescript()` function
0.1.0
* Initial release

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

13
R/logger.R

@ -0,0 +1,13 @@
#' Log a message to the macOS logging system (searchable from Console.app)
#'
#' @param app name of the "application" (makes it easier to search for your log entries)
#' @param message text to write to the log
#' @export
logger <- function(message, app = "mactheknife") {
system2(
command = "/usr/bin/logger",
args = sprintf("%s: %s", app, message[1])
) -> catch
}

1
README.Rmd

@ -19,6 +19,7 @@ A set of tools/methods and data that are geared towards the 'macOS' ecosystem.
The following functions are implemented:
- `airport_scan`: Scan for available wireless network (requires Wi-Fi enabled Mac)
- `applescript`: Execute AppleScript and Return Results
- `kernel_state`: Retrieve kernel state information
- `find_dsstore`: Find and optionally remove '.DS_Store' files on a locally-accessible filesystem
- `read_dsstore`: Read a '.DS_Store' from a file/URL

1
README.md

@ -20,6 +20,7 @@ The following functions are implemented:
- `airport_scan`: Scan for available wireless network (requires Wi-Fi
enabled Mac)
- `applescript`: Execute AppleScript and Return Results
- `kernel_state`: Retrieve kernel state information
- `find_dsstore`: Find and optionally remove ‘.DS\_Store’ files on a
locally-accessible filesystem

BIN
inst/modules/__pycache__/dsstore.cpython-37.pyc

Binary file not shown.

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

16
man/logger.Rd

@ -0,0 +1,16 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/logger.R
\name{logger}
\alias{logger}
\title{Log a message to the macOS logging system (searchable from Console.app)}
\usage{
logger(message, app = "mactheknife")
}
\arguments{
\item{message}{text to write to the log}
\item{app}{name of the "application" (makes it easier to search for your log entries)}
}
\description{
Log a message to the macOS logging system (searchable from Console.app)
}
Loading…
Cancel
Save