Вызов appleScript в R - PullRequest
       9

Вызов appleScript в R

0 голосов
/ 09 октября 2018

Есть ли способ запустить яблочный скрипт в R?

Я нашел эту ссылку в R FAQ по CRAN

From release 1.3.1 R has partial support for AppleScripts. This means two things: you can run applescripts from inside R using the command applescript() (see the corresponding help)

Но в моей текущей версии R

R version 3.4.1 (2017-06-30) Platform: x86_64-apple-darwin15.6.0 (64-bit) Running under: macOS High Sierra 10.13.6

ни applescript(), ни ?applescript() ничего не возвращает.

Спасибо, Саймон

1 Ответ

0 голосов
/ 09 октября 2018

Эти функции отсутствуют в современных версиях R (IIRC они возвращаются к временам до MacOS / Mac OS X).

Однако функция applescript() не выполняла никаких действий:

applescript <- function(script_source, extra_args = 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)

  system2(
    command = osascript,
    args = args,
    stdout = TRUE
  ) -> res

  invisible(res)

}

Таким образом, вы можете делать с ней все что угодно, например, открывать папку:

applescript(
  sprintf(
    'tell app "Finder" to open POSIX file "%s"',
    Sys.getenv("R_DOC_DIR")
  )
)

или,запросить приложение и вернуть данные:

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 & "\ntrack=" & r_name
')

print(res)
## [1] "artist=NICO Touches the Walls" "track=Hologram"

Для (mebbe) более легкого использования (я говорю «mebbe», поскольку pkg для некоторых вещей полагается на reticulate) Я добавил это к macthekinfe MacOS-ориентированный пакет R.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...