Menu

Skip to content
AppleScriptの穴
  • Home
  • Products
  • Books
  • Docs
  • Events
  • Forum
  • About This Blog
  • License
  • 仕事依頼

AppleScriptの穴

Useful & Practical AppleScript archive. Click '★Click Here to Open This Script' Link to download each AppleScript

LAN上の別のMacでYouTubeムービー再生をハンズオーバー v2

Posted on 10月 8, 2020 by Takaaki Naganoya

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

★Click Here to Open This Script 

More from my site

  • eppcで他のMac上のアプリケーション操作eppcで他のMac上のアプリケーション操作
  • eppcによるリモートマシンの操作eppcによるリモートマシンの操作
  • YouTubeムービーの状態を取得、操作YouTubeムービーの状態を取得、操作
  • 表示中のYouTubeのムービーをローカルにダウンロードして音声のみ抽出表示中のYouTubeのムービーをローカルにダウンロードして音声のみ抽出
  • 表示中のYouTubeのムービーをローカルにダウンロード v2表示中のYouTubeのムービーをローカルにダウンロード v2
  • 指定ファイルのメタデータ表示 v2指定ファイルのメタデータ表示 v2
(Visited 134 times, 1 visits today)
Posted in Internet JavaScript Remote Control | Tagged 10.13savvy 10.14savvy 10.15savvy 11.0savvy Finder NSMetadataItem NSURL Safari | 14 Comments

14 thoughts on “<span>LAN上の別のMacでYouTubeムービー再生をハンズオーバー v2</span>”

  1. 10/16/20
    11:27 AM
    2020年10月16日
    11:27 AM

    Reply

    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.

  2. 10/16/20
    4:53 PM
    2020年10月16日
    4:53 PM

    Reply

    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.

    • 10/16/20
      11:09 PM
      2020年10月16日
      11:09 PM

      Reply

      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

  3. 10/17/20
    12:35 AM
    2020年10月17日
    12:35 AM

    Reply

    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).

  4. 10/17/20
    8:55 AM
    2020年10月17日
    8:55 AM

    Reply

    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

  5. 10/17/20
    11:11 AM
    2020年10月17日
    11:11 AM

    Reply

    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
    “`

  6. 10/17/20
    2:49 PM
    2020年10月17日
    2:49 PM

    Reply

    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.

    • 10/17/20
      3:06 PM
      2020年10月17日
      3:06 PM

      Reply

      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?

  7. 10/17/20
    3:29 PM
    2020年10月17日
    3:29 PM

    Reply

    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

    • 10/18/20
      7:10 PM
      2020年10月18日
      7:10 PM

      Reply

      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.

  8. 10/18/20
    11:13 PM
    2020年10月18日
    11:13 PM

    Reply

    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
    “`

    • 10/19/20
      12:09 PM
      2020年10月19日
      12:09 PM

      Reply

      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

    • 10/20/20
      4:05 PM
      2020年10月20日
      4:05 PM

      Reply

      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

  9. 10/21/20
    8:04 AM
    2020年10月21日
    8:04 AM

    Reply

    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

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

電子書籍(PDF)をオンラインストアで販売中!

Google Search

Popular posts

  • macOS 13, Ventura(継続更新)
  • アラートダイアログ上にWebViewで3Dコンテンツを表示(WebGL+three.js)v3
  • UI Browserがgithub上でソース公開され、オープンソースに
  • Xcode 14.2でAppleScript App Templateを復活させる
  • macOS 13 TTS Voice環境に変更
  • 2022年に書いた価値あるAppleScript
  • ChatGPTで文章のベクトル化(Embedding)
  • 新発売:AppleScriptからSiriを呼び出そう!
  • iWork 12.2がリリースされた
  • 従来と異なるmacOS 13の性格?
  • 新発売:CotEditor Scripting Book with AppleScript
  • macOS 13対応アップデート:AppleScript実践的テクニック集(1)GUI Scripting
  • AS関連データの取り扱いを容易にする(はずの)privateDataTypeLib
  • macOS 12.5.1、11.6.8でFinderのselectionでスクリーンショット画像をopenできない問題
  • macOS 13でNSNotFoundバグふたたび
  • ChatGPTでchatに対する応答文を取得
  • 新発売:iWork Scripting Book with AppleScript
  • Finderの隠し命令openVirtualLocationが発見される
  • macOS 13.1アップデートでスクリプトエディタの挙動がようやくまともに
  • あのコン過去ログビューワー(暫定版)

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1390) 10.14savvy (586) 10.15savvy (434) 11.0savvy (277) 12.0savvy (185) 13.0savvy (55) CotEditor (60) Finder (47) iTunes (19) Keynote (98) NSAlert (60) NSArray (51) NSBezierPath (18) NSBitmapImageRep (20) NSBundle (20) NSButton (34) NSColor (51) NSDictionary (27) NSFileManager (23) NSFont (18) NSImage (41) NSJSONSerialization (21) NSMutableArray (62) NSMutableDictionary (21) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (117) NSURL (97) NSURLRequest (23) NSUTF8StringEncoding (30) NSView (33) NSWorkspace (20) Numbers (56) Pages (37) Safari (41) Script Editor (20) WKUserContentController (21) WKUserScript (20) WKUserScriptInjectionTimeAtDocumentEnd (18) WKWebView (23) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • beta
  • Bluetooth
  • Books
  • boolean
  • bounds
  • Bug
  • Calendar
  • call by reference
  • Clipboard
  • Code Sign
  • Color
  • Custom Class
  • dialog
  • drive
  • exif
  • file
  • File path
  • filter
  • folder
  • Font
  • Font
  • GAME
  • geolocation
  • GUI
  • GUI Scripting
  • Hex
  • History
  • How To
  • iCloud
  • Icon
  • Image
  • Input Method
  • Internet
  • iOS App
  • JavaScript
  • JSON
  • JXA
  • Keychain
  • Keychain
  • Language
  • Library
  • list
  • Locale
  • Machine Learning
  • Map
  • Markdown
  • Menu
  • Metadata
  • MIDI
  • MIME
  • Natural Language Processing
  • Network
  • news
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • PDF
  • Peripheral
  • PRODUCTS
  • QR Code
  • Raw AppleEvent Code
  • Record
  • rectangle
  • recursive call
  • regexp
  • Release
  • Remote Control
  • Require Control-Command-R to run
  • REST API
  • Review
  • RTF
  • Sandbox
  • Screen Saver
  • Script Libraries
  • sdef
  • search
  • Security
  • selection
  • shell script
  • Shortcuts Workflow
  • Sort
  • Sound
  • Spellchecker
  • Spotlight
  • SVG
  • System
  • Tag
  • Telephony
  • Text
  • Text to Speech
  • timezone
  • Tools
  • Update
  • URL
  • UTI
  • Web Contents Control
  • WiFi
  • XML
  • XML-RPC
  • イベント(Event)
  • 未分類

アーカイブ

  • 2023年9月
  • 2023年8月
  • 2023年7月
  • 2023年6月
  • 2023年5月
  • 2023年4月
  • 2023年3月
  • 2023年2月
  • 2023年1月
  • 2022年12月
  • 2022年11月
  • 2022年10月
  • 2022年9月
  • 2022年8月
  • 2022年7月
  • 2022年6月
  • 2022年5月
  • 2022年4月
  • 2022年3月
  • 2022年2月
  • 2022年1月
  • 2021年12月
  • 2021年11月
  • 2021年10月
  • 2021年9月
  • 2021年8月
  • 2021年7月
  • 2021年6月
  • 2021年5月
  • 2021年4月
  • 2021年3月
  • 2021年2月
  • 2021年1月
  • 2020年12月
  • 2020年11月
  • 2020年10月
  • 2020年9月
  • 2020年8月
  • 2020年7月
  • 2020年6月
  • 2020年5月
  • 2020年4月
  • 2020年3月
  • 2020年2月
  • 2020年1月
  • 2019年12月
  • 2019年11月
  • 2019年10月
  • 2019年9月
  • 2019年8月
  • 2019年7月
  • 2019年6月
  • 2019年5月
  • 2019年4月
  • 2019年3月
  • 2019年2月
  • 2019年1月
  • 2018年12月
  • 2018年11月
  • 2018年10月
  • 2018年9月
  • 2018年8月
  • 2018年7月
  • 2018年6月
  • 2018年5月
  • 2018年4月
  • 2018年3月
  • 2018年2月

https://piyomarusoft.booth.pm/items/301502

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org

Forum Posts

  • 人気のトピック
  • 返信がないトピック

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org
Proudly powered by WordPress
Theme: Flint by Star Verte LLC