Skip to content

Instantly share code, notes, and snippets.

@inside-hakumai
Last active November 7, 2017 06:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inside-hakumai/8456c4e003c069adc1435be0f4c2a2e2 to your computer and use it in GitHub Desktop.
Save inside-hakumai/8456c4e003c069adc1435be0f4c2a2e2 to your computer and use it in GitHub Desktop.
iTerm2のウインドウサイズに応じて最適なアスペクト比の背景画像を設定する
ObjC.import("stdlib");
// 用意した画像の縦横比(縦方向の長さを1とした時のに対する横方向の長さ)
var ratio_cands = [0.5, 0.6, 0.7, 0.8, 0.9,
1.0, 1.1, 1.2, 1.3, 1.4,
1.5, 1.6, 1.7, 1.8, 1.9,
2.0, 1.33, 1.78];
while(true) {
if (Application("iTerm").running()){
// アクティブなiTermウインドウの縦横の長さを取得する
var iTerm = Application("iTerm");
var window = iTerm.currentWindow;
var windowBounds = window.bounds();
var width = windowBounds.width;
var height = windowBounds.height - 25;
// ウインドウの縦横比を計算
var ratio = width/height;
// iTermのウインドウの縦横比に対して、用意した画像の縦横比から最も近い値を取得
var nearest_val = null;
var minimum_diff = Number.MAX_VALUE;
for (var i = 0; i < ratio_cands.length; i++) {
var diff = Math.abs(ratio_cands[i] - ratio);
if (diff < minimum_diff) {
nearest_val = ratio_cands[i];
minimum_diff = diff;
}
}
// 背景画像を設定
var profile_name = window.currentSession.profileName();
var current_bg_path = window.currentSession.backgroundImage();
var bg_dirpath = current_bg_path.substring(0, current_bg_path.lastIndexOf("/"));
window.currentSession.backgroundImage
= bg_dirpath + "/" + profile_name + "_" + nearest_val.toFixed(2) + ".png";
// 5秒毎にループ
delay(5);
} else {
$.exit(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment