Create a gist now

Instantly share code, notes, and snippets.

@shokai /dot.zshrc
Last active Aug 29, 2015

What would you like to do?
Enterキー押す毎にJXAでiTerm2の背景を変える http://shokai.org/blog/archives/10159
function iterm-bg-image-random(){
zle accept-line
if [[ -z "$BUFFER" ]]; then
$(iterm-bg-image ~/Pictures/写真\ Library.photoslibrary -r > /dev/null 2>&1 &)
fi
}
zle -N iterm-bg-image-random
bindkey '^m' iterm-bg-image-random
#!/usr/bin/osascript
on run argv
set img to item 1 of argv
tell application "iTerm"
tell the current terminal
tell the current session
set background image path to img
end tell
end tell
end tell
end run
#!/usr/bin/osascript -l JavaScript
var fm = $.NSFileManager.defaultManager;
function run(argv){
var [path, opts] = parseArgv(argv);
var img = getImagePath(path, opts.r);
console.log(img);
setImage(img);
}
function parseArgv(argv){
var path = null;
var opts = {};
argv.forEach(function(arg){
var match = arg.match(/^\-+(.+)$/);
if(match){
opts[match[1]] = true;
}
else{
path = arg;
}
});
return [path, opts];
}
function getImagePath(path, recursive){
if(!path) return;
var ref = Ref();
var exists = fm.fileExistsAtPathIsDirectory(path, ref);
if(!exists) return;
var isDirecotry = ref[0];
if(!isDirecotry) return path;
var files = getImagesInDirectory(path, recursive);
return files[Math.floor(Math.random()*files.length)];
}
function getImagesInDirectory(path, recursive){
path = path.replace(/\/$/, "");
var app = Application.currentApplication();
app.includeStandardAdditions = true;
var files = [];
if(recursive){
files = app.doShellScript("find '"+path+"'").split(/\r/);
}
else{
files = app.doShellScript("ls '"+path+"'").split(/\r/)
.map(function(f){
return path+"/"+f;
});
}
return files.filter(function(f){
return /\.(jpe?g|gif|png|bmp)$/i.test(f);
});
}
function setImage(img){
var app = Application("iTerm");
app.currentTerminal.currentSession.backgroundImagePath = img || null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment