Skip to content

Instantly share code, notes, and snippets.

@mkearney
Forked from dkiesow/keynote.R
Last active November 16, 2019 17:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkearney/4ca554a1e812260b47d88c3bb6da9798 to your computer and use it in GitHub Desktop.
Save mkearney/4ca554a1e812260b47d88c3bb6da9798 to your computer and use it in GitHub Desktop.
R script used with rtweet and Keynote rTweet AppleScript to automate tweets from Mac Keynote
#!/usr/bin/env Rscript
##keynote.R v1.0 Damon Kiesow @dkiesow
##Use with the Keynote rTweet AppleScript app to automate threaded tweeting during Keynote presentations
##
## load rtweet package
library(rtweet)
me <- rtweet:::home_user()
## Pull parameters from command line (first_status will be "yes" or "no" and provided from the AppleScript)
args <- commandArgs(trailingOnly = TRUE)
## Check to make sure there are two parameters
if (length(args) < 2) {
stop("Two arguments must be supplied")
} else if (length(args) == 2) {
keynote_status <- args[1]
first_status <- args[2]
}
## Ensure correct encoding by (a) setting R's default encoding
op <- getOption("encoding")
on.exit(options(encoding = op))
options(encoding = "UTF-8")
## and (b) convert bytes in string to utf8 chars
bytes2char <- function(x) {
m <- gregexpr("<[[:alnum:]]{2}>", x)
y <- regmatches(x, m)[[1]]
y <- gsub("^<|>$", "", y)
y <- strsplit(y, "[><]+")
regmatches(x, m) <- list(sapply(y, function(.x) rawToChar(as.raw(paste0("0x", .x)))))
x
}
keynote_status <- bytes2char(keynote_status)
## Send a new tweet if first_status is "Yes" and reply tweet if "No"
## If you neglect to set the "[first]" flag in Keynote it will thread from your most recent tweet
## rtweet uses "get_timeline" to pull the last 3 tweets from your account
## It then uses "reply_id" to pull your last tweet's "status_id" from the data table of "my_timeline"
if (first_status == 'yes') {
post_tweet(status = keynote_status)
} else {
my_timeline <- get_timeline(user = me, 3)
reply_id <- my_timeline[1, 2]
post_tweet(status = keynote_status, in_reply_to_status_id = reply_id)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment