Last active
August 29, 2015 14:23
-
-
Save shokai/872e375c0b0636b0c2ae to your computer and use it in GitHub Desktop.
Enterキー押す毎にJXAでiTerm2の背景を変える http://shokai.org/blog/archives/10159
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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