Skip to content

Instantly share code, notes, and snippets.

@Cojad
Last active February 22, 2017 04:43
Show Gist options
  • Save Cojad/a0f57aaa9e6ecea14b8faedfb74bcb4e to your computer and use it in GitHub Desktop.
Save Cojad/a0f57aaa9e6ecea14b8faedfb74bcb4e to your computer and use it in GitHub Desktop.
設定時間自動在OSX對Line視窗按下Enter按鍵
#!/usr/bin/php
<?php
$stage=0; //如果不要有語音提醒把 $stage=0; 改成 $stage=1;
$USA=false;
($USA) ? date_default_timezone_set("America/New_York") : date_default_timezone_set("Asia/Taipei");
echo
"==================== 自動切換至line視窗, 並且按下Enter ========================
+ usage: line
+ 在下一個分零秒整, 切換到line視窗, 並且按下Enter
+ usage: line 16:30
+ 在16:30:00秒整, 切換到line視窗, 並且按下Enter
+ usage: line 16:30:10
+ 在16:30:10秒整, 切換到line視窗, 並且按下Enter
+
+ PS1. Line視窗可以不用在背景/隱藏/縮小到Docker, 但是請 -- 不要關閉 -- Line視窗
+ PS2. Line請先設定成單一視窗模式
+ Creator: Cojad
===============================================================================\n\n";
$time = (count($argv)>1) ? $argv[1] : date("H:i",strtotime("now + 1 minute"));
$mtime = (count($argv)>2) ? $argv[2] : 0;
$timeUnix = strtotime($time);
//計算毫秒
if($mtime>1 or $mtime<-1){
$timeUnix+=(int)$mtime;
$mtime-=(int)$mtime;
}
//如果負數毫秒, 改為正數
if($mtime<0) {
$timeUnix--;
$mtime++;
}
function mt() {
global $mtime;
list($usec, $sec) = explode(" ", microtime());
return ( (float)$usec + (float)$sec );
}
//echo "指令將於 " . date("H:i:s",$timeUnix) . " 執行\n";
$t=$timeUnix;
$s=$t%60;
printf("指令將於 %s:%05.2f 執行\n",date("H:i",$timeUnix),$s+$mtime);
while ( $timeUnix > mt() ) { //提前0.2秒執行, 來確定Line視窗被叫出來動畫跑完
$t=mt();
$s=$t%60;
if(mt() > $timeUnix-30 && $stage<1 ) {
//30秒之前, 用VoiceOver告知使用者, 即將切換視窗
printf("現在時間 %s:%05.2f\n",date("H:i"),$s+$t-time());
if($USA){
system("echo \"Voice Notify: 30 seconds before activation Line Program Enter key procedure, Please do not use computer for 30 seconds.\"");
system("say -r 300 \"30 seconds before activation Line Program Enter key procedure, Please do not use computer for 30 seconds.\"");
} else {
system("echo \"語音提示: 30 秒後執行 Line 視窗的 Enter 程序, 請暫時不要使用電腦\"");
system("say -r 300 \"30 秒後執行 Line 視窗的 Enter 程序, 請暫時不要使用電腦\"");
}
$stage=1;
}
if(mt() > $timeUnix-2 && $stage<2 ) {
//2秒前, 預先叫出line視窗
system("osascript -e 'tell app \"LINE\" to reopen'");
system("osascript -e 'activate application \"LINE\"'");
$stage=2;
}
printf("現在時間 %s:%05.2f",date("H:i"),$s+$t-time());
usleep(10000);
echo "\r";
}
$t=mt();
$s=$t%60;
printf("現在時間 %s:%05.2f\n",date("H:i"),$s+$t-time());
//送出Enter按鍵
system("osascript -e 'tell application \"System Events\" to tell process \"LINE\" to key code 76'");
$t=mt();
$s=$t%60;
printf("程式結束 %s:%05.2f\n",date("H:i"),$s+$t-time());
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment