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

YouTubeムービー再生実験

Posted on 11月 13 by Takaaki Naganoya

macOS 26上でdisplay youtube AppleScriptライブラリの機能を利用したAppleScriptを、macOS標準搭載の「スクリプトメニュー」から呼び出すと、

のようにエラー表示されるようになっていました。

macOS 26.0βのときには再生されたりしていたのですが、リリース後のmacOS 26.2では再生されません。

これが、macOSの内部機能やポリシーの変更によるものなのか、あるいはmacOS 26のデフォルト設定の変化によるものかは不明です。YouTube側の仕様変更という疑いもありますが、定かではありません。

以前は文字列で組み立てたHTMLをWkWebViewに読み込ませていましたが、いまmacOS 26上ではそのやり方ではYouTubeのムービーは再生されません。

そこで、根本的な解決を図るため、local web server(httpd)を介してHTMLを表示する方式に変更。docrootをtemporary items folderに設定したhttpdを起動。temporary items folderに書き込んだhtmlをダイアログ上のWkWebViewに表示するようにしました。

このlocal web serverの起動にpython3を呼び出しており、インストールされていない環境(一般的な環境ではインストールされていない)ではDeveloper Toolsのインストールが実行初回時に行われます。一般ユーザーのことを考えるとPython3以外のツールを使って起動すべきなんでしょう。

以前のバージョンと異なるのは、ダイアログ上のWkWebViewに再生用のUI(中央にYouTUbeロゴ)が配置され、それをクリックしないとムービーの再生が始まらないことです。このあたりは、自動再生を開始できるように改変できそうではあるものの、同時にデメリット(音声の再生が行えないとか)が生じるおそれがあるため、現状のままがよさそうです。

それでも、macOS 26上でWkWebViewでYouTubeムービーを再生させられるようになりました。まだ、繰り返し実行して問題が出ないかといったテストを行なっている段階です。

htmlファイルの書き込みが間に合わないと上記のようなエラーが出ていましたが、処理の冒頭で明示的にhttpdをkillすることで、この問題を解消できるようになりました。httpdのkillについては、他のプログラムが起動していた場合には問題になりそうなので、そこも様子見といったところでしょう。

ちなみに、YouTubeムービー再生以外の用途では、とくにこのような大幅な改変は必要ではないと認識しています。たとえば、CDN上のJavaScriptライブラリを呼び出してアニメーション表示つきのグラフ表示をAppleScriptで行なっていますが、こちらでは書き換えは必要ありませんでした。

AppleScript名:youtubeムービー再生実験.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2025/11/12
—
–  Copyright © 2025 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use framework "WebKit"
use scripting additions

property |NSURL| : a reference to current application’s |NSURL|
property WKWebView : a reference to current application’s WKWebView
property NSURLRequest : a reference to current application’s NSURLRequest
property NSRunningApplication : a reference to current application’s NSRunningApplication
property NSUTF8StringEncoding : a reference to current application’s NSUTF8StringEncoding
property WKWebViewConfiguration : a reference to current application’s WKWebViewConfiguration

property returnCode : 0

set mainMessage to "Youtube Test"
set subMessage to "Using local Web Server"
set viewSizeArray to {1200, 640}

set youtubeMovieID to "GP_tVXTYdmY"

–テスト用HTML文字列
–set htmlString to "<!DOCTYPE html><html><body><h1>Hello World</h1></body></html>"

— HTML文字列
set htmlString to "
<!DOCTYPE html>
<html lang=\"ja\">
<head>
<meta charset=\"UTF-8\">
<title>YouTube Embed</title>
<style>
body { margin:0; padding:0; }
iframe { width:100%; height:100vh; border:none; }
</style>
</head>
<body>
<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/"
& youtubeMovieID & "?start=1\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>
</body>
</html>
"

try
  do shell script "pkill -f ’http.server’ || true"
end try

set docroot to POSIX path of (path to temporary items)

–1. Python3でバックグラウンドサーバー起動(Local Web Server)
set pythonCmd to "python3 -m http.server 8000 –directory " & quoted form of docroot & " >/dev/null 2>&1 & echo $!"
set pidStr to do shell script pythonCmd
set serverPID to pidStr as integer

— 2. HTMLを書き出す
set htmlPath to docroot & "index.html"
set nsHTML to current application’s NSString’s stringWithString:htmlString
nsHTML’s writeToFile:htmlPath atomically:true encoding:(current application’s NSUTF8StringEncoding) |error|:(missing value)

— 3. 少し待ってサーバー起動を待機
delay 0.5

try
  — 4. WKWebViewでHTTP URLを読み込む
  
set httpURL to "http://localhost:8000/index.html"
  
set paramObj to {myMessage:mainMessage, mySubMessage:subMessage, viewSize:viewSizeArray, htmlStr:"", myURL:httpURL}
  
  
–my browseURLContents:paramObj
  
my performSelectorOnMainThread:"browseURLContents:" withObject:(paramObj) waitUntilDone:true
on error errMsg
  display dialog errMsg
end try

— 5. サーバープロセス終了
try
  do shell script "pkill -f ’http.server’ || true"
end try

— WKWebView表示用ハンドラ
on browseURLContents:paramObj
  set webSize to viewSize of paramObj
  
copy webSize to {aWidth, aHeight}
  
  
set aURL to myURL of paramObj
  
  
set aConf to WKWebViewConfiguration’s alloc()’s init()
  
set aWebView to WKWebView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight)) configuration:aConf
  
aWebView’s setNavigationDelegate:me
  
aWebView’s setUIDelegate:me
  
aWebView’s setTranslatesAutoresizingMaskIntoConstraints:true
  
  
set bURL to |NSURL|’s URLWithString:aURL
  
set bReq to NSURLRequest’s requestWithURL:bURL
  
aWebView’s loadRequest:bReq
  
  
— Move to frontmost
  
current application’s NSApplication’s sharedApplication()’s setActivationPolicy:(current application’s NSApplicationActivationPolicyRegular)
  
current application’s NSApp’s activateIgnoringOtherApps:true
  
  
— アラートに埋め込む
  
set theAlert to current application’s NSAlert’s alloc()’s init()
  
tell theAlert
    –addButtonWithTitle_("Cancel")
    
its setMessageText:(myMessage of paramObj)
    
its setInformativeText:(mySubMessage of paramObj)
    
its addButtonWithTitle:"OK"
    
its setAccessoryView:aWebView
    
set myWindow to its |window|
    
tell myWindow
      its setLevel:(current application’s NSScreenSaverWindowLevel)
      
setInitialFirstResponder_(aWebView)
    end tell
  end tell
  
  
— モーダル表示
  
NSRunningApplication’s currentApplication()’s activateWithOptions:0
  
set returnCode to (theAlert’s runModal()) as number
  
  
— WKWebViewの後始末
  
set jsStop to "try { document.getElementById(’ytplayer’).contentWindow.postMessage(’{\"event\":\"command\",\"func\":\"stopVideo\",\"args\":\"\"}’, ’*’); } catch(e) {}"
  
aWebView’s evaluateJavaScript:jsStop completionHandler:(missing value)
  
delay 0.1
  
aWebView’s loadRequest:(NSURLRequest’s requestWithURL:(|NSURL|’s URLWithString:"about:blank"))
  
delay 0.1
  
aWebView’s stopLoading()
  
set aWebView to missing value
  
  
set aWebView to missing value
end browseURLContents:

★Click Here to Open This Script 

(Visited 3 times, 3 visits today)
Posted in dialog Web Contents Control | Tagged 26.0savvy | Leave a comment

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

  • Numbersで選択範囲のセルの前後の空白を削除
  • macOS 26, Tahoe
  • macOS 15でも変化したText to Speech環境
  • KagiのWebブラウザ、Orion
  • Script Debuggerの開発と販売が2025年に終了
  • 【続報】macOS 15.5で特定ファイル名パターンのfileをaliasにcastすると100%クラッシュするバグ
  • macOS 15 リモートApple Eventsにバグ?
  • NSObjectのクラス名を取得 v2.1
  • 2024年に書いた価値あるAppleScript
  • macOS 15:スクリプトエディタのAppleScript用語辞書を確認できない
  • Xcode上のAppleScriptObjCのプログラムから、Xcodeのログ欄へのメッセージ出力を実行
  • (確認中)AppleScript Dropletのバグっぽい動作が解消?
  • AppleScript Dropletのバグっぽい動作が「復活」(macOS 15.5β)
  • AVSpeechSynthesizerで読み上げテスト
  • macOS 26, 15.5でShortcuts.app「AppleScriptを実行」アクションのバグが修正される
  • 指定フォルダ以下の画像のMD5チェックサムを求めて、重複しているものをピックアップ
  • Numbersで選択中の2列のセルを比較して並べ直して書き戻す v2
  • Apple、macOS標準搭載アプリ「写真」のバージョン表記を間違える
  • Script Debuggerがフリーダウンロードで提供されることに
  • macOS 15.5beta5(24F74)でaliasのキャスティングバグが修正された???

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1391) 10.14savvy (587) 10.15savvy (438) 11.0savvy (283) 12.0savvy (212) 13.0savvy (204) 14.0savvy (159) 15.0savvy (158) 26.0savvy (22) CotEditor (66) Finder (52) Keynote (119) NSAlert (61) NSArray (51) NSBitmapImageRep (20) NSBundle (20) NSButton (34) NSColor (53) NSDictionary (28) NSFileManager (23) NSFont (21) NSImage (41) NSJSONSerialization (21) NSMutableArray (63) NSMutableDictionary (22) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (119) NSURL (98) NSURLRequest (23) NSUTF8StringEncoding (30) NSView (33) NSWorkspace (20) Numbers (76) Pages (56) Safari (44) Script Editor (27) WKUserContentController (21) WKUserScript (20) WKWebView (23) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • Beginner
  • Benchmark
  • beta
  • Bluetooth
  • Books
  • boolean
  • bounds
  • Bug
  • Calendar
  • call by reference
  • check sum
  • Clipboard
  • Cocoa-AppleScript Applet
  • Code Sign
  • Color
  • Custom Class
  • date
  • dialog
  • diff
  • drive
  • Droplet
  • 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
  • Localize
  • Machine Learning
  • Map
  • Markdown
  • Menu
  • Metadata
  • MIDI
  • MIME
  • Natural Language Processing
  • Network
  • news
  • Newt On Project
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • parallel processing
  • PDF
  • Peripheral
  • process
  • 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
  • Scripting Additions
  • 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)
  • 未分類

アーカイブ

  • 2025年11月
  • 2025年10月
  • 2025年9月
  • 2025年8月
  • 2025年7月
  • 2025年6月
  • 2025年5月
  • 2025年4月
  • 2025年3月
  • 2025年2月
  • 2025年1月
  • 2024年12月
  • 2024年11月
  • 2024年10月
  • 2024年9月
  • 2024年8月
  • 2024年7月
  • 2024年6月
  • 2024年5月
  • 2024年4月
  • 2024年3月
  • 2024年2月
  • 2024年1月
  • 2023年12月
  • 2023年11月
  • 2023年10月
  • 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