Safariの最前面のウィンドウで再生中のYouTubeムービーの情報を取得し、LAN上の別のMacで再生を引き継ぐ(ハンズオーバーする)AppleScriptです。
macOS 10.13以降、リモートAppleEvents経由でGUIアプリケーションを直接操作する機能が復活しました(Mac OS X 10.7〜10.12ぐらいまでAppleScriptアプレット間のみリモート通信が許可されていた状態)。
メインマシンで再生中のYouTubeムービーを、LAN上の他のマシンに引き継がせてみました。再生を引き継がれる方のマシンでは、システム環境設定の「共有」で「リモートApple Events」の項目をオンにしています(自分のマシンではすべてこの項目をオンにしています)。
(1)リモートマシン上のユーザーのパスワード
AppleScript書類のコメント(Finderコメント)にパスワードを書いておくと、それを読み取って使用するようにしてみました。
(2)リモートマシン上のSafariの起動
リモートマシン上のアプリケーションの操作は、ただリモートマシン上のアプリケーションを指定すればOKなのですが、操作対象のアプリケーションが起動していない場合にはエラーになります。これは、とても困る仕様です。
そこで、リモートマシンのFinder経由でアプリケーションファイルをオープンすることで、リモートマシン上でSafariを起動します。オープン対象をapplication file “Safari”と指定するとエラーになりますが、application file id “com.apple.Safari”と指定するとエラーになりません。
(3)YouTubeで再生中の情報取得
以前調査しておいた内容をそのまま使っています。再生中ならPauseし、再生中の位置(時間)情報を取得し、文字列で指定するために加工してYouTubeのURLに追加しています。URLの加工部分は少々手抜きをしています。
とくに問題なく、メインマシンから他のマシン(macOS 10.15.7/macOS 11.0beta9)にLAN経由で再生をハンズオーバーできました。
実際に、コントロール先のマシン名(Bonjour名)をremoteMachineNameに、ユーザー名をremoteUserNameに、パスワードを実行するAppleScript書類のFinderコメントに書き込んで実行してください。スクリプトエディタ上でもスクリプトメニューからでも問題なく実行できています。
あとは、Safari上のYouTube再生をフルスクリーンで行えるとよいのですが、少し試した範囲ではできなかったので、また地道に調べておく感じでしょうか。
AppleScript名:LAN上の別のMacでYouTubeムービー再生をハンズオーバー v2.scpt |
— – Created by: Takaaki Naganoya – Created on: 2020/10/08 — – Copyright © 2020 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use scripting additions property |NSURL| : a reference to current application’s |NSURL| property NSMetadataItem : a reference to current application’s NSMetadataItem set remoteMachineName to "MacMini2014.local" set remoteUserName to "maro" –ScriptのCommentに書いておいたパスワードを拾って使う set mePath to (path to me) –FinderからCommentは拾えるが、メタデータ経由で取得する処理も試してみた set remoteUserPass to getFinderComment(POSIX path of mePath) of me tell application "Safari" if running then tell front document set aURL to URL –最前面のウィンドウがYouTubeの場合のみ処理 if aURL does not start with "https://www.youtube.com/" then display notification "エラー: YouTubeを再生していないため、ハンズオーバーしませんでした" return end if –再生中のポジションを取得 set tRes to (do JavaScript "document.querySelector(’#movie_player video’).currentTime;") –再生状況を取得 set pRes to (do JavaScript "document.querySelector(’#movie_player video’).paused;") if pRes = false then –再生中であればPauseする set aRes to (do JavaScript "document.querySelector(’#movie_player .ytp-play-button’).click();") end if openYouTubeOnRemoteMachine(remoteUserName, remoteUserPass, remoteMachineName, tRes, aURL) of me end tell end if end tell –指定のリモートマシン上のSafariでYouTubeの指定ムービーの指定箇所からの再生を行う on openYouTubeOnRemoteMachine(remoteUser, remotePass, remoteMachineLocal, newDuration, newURL) set remoteMachineName to "eppc://" & remoteUser & ":" & remotePass & "@" & remoteMachineLocal –URLの加工。ちょっと手抜きをした if newDuration is not 0 then set tText to retTimeText(newDuration) of me if newURL contains "&" then set sepChar to "?" else set sepChar to "&" end if set newURL to newURL & sepChar & "t=" & tText end if using terms from application "Safari" tell application "Safari" of machine remoteMachineName if not running then –起動していなかったらあらためてSafariを起動 launchRemoteSafari(remoteMachineName) of me end if try close every document end try set aWin to make new document tell aWin set URL to newURL –フルスクリーン再生をためしてみたが、こういう書き方ではなかった模様(URLオープンを待つ必要もある) –set aRes to (do JavaScript "document.querySelector(’#movie_player playFullscreen’).click();") end tell end tell end using terms from end openYouTubeOnRemoteMachine –リモートマシン上でSafariを起動する on launchRemoteSafari(aMachine) using terms from application "Finder" tell application "Finder" of machine aMachine open application file id "com.apple.Safari" end tell end using terms from end launchRemoteSafari –数値を「h」「m」「s」でフォーマットして返す on retTimeText(aTime) set aHour to aTime div 3600 set aMinute to (aTime – (aHour * 3600)) div 60 set aSeconds to (aTime mod 60) set aString to "" if aHour > 0 then set aString to aHour & "h" end if if aMinute > 0 then set aString to aString & (aMinute as integer) & "m" end if if aSeconds > 0 then set aString to aString & (aSeconds as integer as string) & "s" end if return (aString as string) end retTimeText –Finderコメントをメタデータ経由で取得 on getFinderComment(aPOSIX) set aURL to |NSURL|’s fileURLWithPath:aPOSIX set aMetaInfo to NSMetadataItem’s alloc()’s initWithURL:aURL set metaDict to (aMetaInfo’s valuesForAttributes:{"kMDItemFinderComment"}) as record if metaDict = {} then return "" set aComment to kMDItemFinderComment of (metaDict) return aComment end getFinderComment |
guilehong says:
hello~ maro. I am sincerely a fan of you.
There are many AppleScript experts, including shane, but I believe you are one of them.
Nothing else, I’m trying to ask a question because EPPC doesn’t work in my catalina 10.15.7
When I did it with the script I wrote, it didn’t work, so I tried it with the handover script you made recently and it doesn’t work either.( error “Error in Safari: Remote access is not allowed.” number -905 )
(1) “defaults write /Library/Preferences/com.apple.AEServer RestrictAccessToUserSession -bool false”
(2) csrutil enable/disable
(3) client/server’s uid, apple id, os version is all same
(4) firewall off
(5) target mac’s specific app is opened ahead
(6) enable remote’s apple event on preferences
Where am I missing?
please answer about my question.
Takaaki Naganoya says:
Hmm…my 10.15.7 environment accept eppc:// control via local WiFi network.
I doubt the user account is not a admin account.
Check connection among them like File Sharing, display sharing.
You can test eppc by controling AppleScript applet on 10.15.7 machine.
Rebooting 10.15.7 machine may work.
Who is calling ? I called 10.15.7 and 11.0 machine from 10.14.6. But I did not have a test calling from 10.15.7 machine.
guilehong says:
Hello~ Maro
Thank you very much for your quick reply
Each user has administrator privileges.
Mainly, these errors are occurring.
error “Error in Finder: Unable to authenticate user.” number -927
error “Error in Safari: Remote access is not allowed.” number -905
Is your test environment client (10.14.6) and server (10.15.7)?
When I test it, my high sierra 10.13.3(client), catalina 10.15.7(server) screen sharing works well without any problems.
Let me know your main settings, and even the slightest things will help me.
It would be nice to tell the client and server respectively.
(1)SIP enable/diable, (2)firewall on/off, (3)os version, (4)etc
I will be very crazy.
I eagerly await your help
thanks…
guilehong
Takaaki Naganoya says:
Did you change remote username, remote machine name and password in Script’s Finder comment?
This script indicate the target machine with hard coding. You have to change target machine name and remote user name.
And remote password (you can set your remote password directly to variable remoteUserPass).
guilehong says:
Hello~, maro
Of course. Everything you said is checked. No problem
Not only your handover, but my eppc scripts don’t work
I strongly believe that this problem I’m experiencing is due to Apple’s enhanced security policy about mac.
I would like you to inform me of your test environment of the following items each client, server.
(1)SIP enable/diable, (2)firewall on/off, (3)os version, (4)etc
guilehong says:
Hello~, maro
The problem was solved so absurdly.
I just created a new user account and I checked that it works..
I don’t know why not with my existing user account.
I have another question.
Is it possible to use the ‘keystroke’ command on the remote server?
If I enter the keystroke command like the code below, number -1728 is returned.
It works fine without this one line of code
“`script
with timeout of 30 seconds
using terms from application “Finder”
tell application “Google Chrome” of machine remoteMachine
activate
tell application “System Events” of machine remoteMachine
tell process “Google Chrome”
keystroke “hello world!” <- – – – – – – – attention!
say (the clipboard)
end tell
end tell
end tell
end using terms from
end timeout
“`
Takaaki Naganoya says:
Hi guilehong,
I’m grad to hear the solution.
And it is a valuable information. In macOS 10.10, we faced to the trouble of Folder action scripting and we had to solve it by making new user account.
System Events (GUI Scripting) control via eppc?
Hmm….I prefer safer way to eppc.
I’ll make GUI Scripting AppleScript applet and run it on remote machine.
Then controller machine send execute message to remote applet.
guileschool says:
Hello, Maro
(Question 1)
Although you didn’t recommend it, is it possible what I asked first?
(Question 2)
According to your words, does it mean that I create an AppleScript app and run the app on the remote server after notarization with SD Notary?
If so, I think it means that those have to talk to each other with applet and the script on the client side.
Do you have any examples of this?
guilehong says:
Hello, Maro
(Question 1)
Is the question I previously asked technically possible?
I want to know how to solve it. Can you tell me?
(Question 2)
I can make a remote applet.
However, I don’t know how to communicate with that applet on the client.
Can you show me related samples?
(Question 3)
I want to know how to ‘enable GUI scripting’ in MacOS Catalina
Ask for an answer
Thanks
Takaaki Naganoya says:
(Answer 1)
>tell process “Google Chrome”
>keystroke “hello world!”
>end tell
This is a mistake. keystroke command requires the target GUI element. If you want to input to some input field on Safari web contents, at first, you have to select the target object. The best way to input text field of web contents is to use do shell script command not GUI Scripting.
We (Japanese users) are nervous about keystroke. Because it is captured by text input method. If Kana-Kanji transfter mode is set, the keystroke characters are convert to Kanji characters. So, If we want to input text field by keystroke command, I strongly recommend to use paste from clipboard.
(Answer 2)
Communicate with remote applet is not difficult.
http://piyocast.com/as/archives/7690
To pass the Script Editor’s compile action, you have to place remote applet to controller (local) machine. “using terms from” is the key technique.
using terms from application “Remote Applet” –same applet on controller machine have to be placed
tell application “Remote Applet” of machine “eppc://remotemachine”
–call some handler
testMe()
end tell
end tell
(Answer 3)
As usual. System Prefetences.app > Security & Privacy > Privacy > Accessibility
Now, we can not change this settings like earlier version of Mac OS X.
guilehong says:
Hello, maro
I’ve been wandering for days now.
Applet control using eppc is an unfamiliar for me.
So, I’d really appreciate it if you could explain elaborately .
Does applet not have to do notarization?
Does not work with error(error “Error in testMe: application is not running.” number -600)
my applet source code is here
“`
on run {yourMessage}
my testMe(yourMessage)
display dialog yourMessage buttons {“A”, “B”}
end run
on testMe(mystr)
say mystr
return mystr
end testMe
“`
Takaaki Naganoya says:
Remote applet does not need to notarize.
Error “application is not runnning” is the error to compile AppleScript without applet on controller machine.
Avoiding this error requires using terms from phrase.
You have to show the caller script to solve.
Or, do you need the description about whole eppc story?
http://piyocast.com/as/wp-content/uploads/2020/10/eppc_with_aplet.png
Takaaki Naganoya says:
eppc in macOS 10.15 has to be same user name both controller machine and remote machine, I forgot.
All my machine environment have same user name.
If you want to talk eppe between different user name account, you have to execute as following…
defaults write /Library/Preferences/com.apple.AEServer RestrictAccessToUserSession -bool false
guilehong says:
Hello, maro
The caller script is the same as the source you showed in the following link
http://piyocast.com/as/archives/7690
Can you show me a test source for caller and callee?
Then, I think the problem will be solved faster