メイン環境をmacOS 26にアップデートしたところ、display youtubeライブラリが動作しなくなっていたので、対処のために作った試作品の改良版です。
# 2a→2bで機能に関係ない範囲の微修正をしました
# 2b→2cでレスポンシブルモードで表示するようにHTML部分を変更しました

edama2さんからYouTubeムービー再生用の改良Scriptを送っていただいて、なんで動かなくなっていたのかの理由がわかりました。HTMLを文字列で与えるのは、問題ないものの……リファラーを「https://www.youtube.com」ではなく「https://www.google.com」にする必要があったのだとか(検索エンジンの検索結果から表示できるように、という話に見えます)。
結局、local web serverを起動する必要はなかったので起動せず、起動していないのでkillもせず、htmlの書き出しも行わないのでファイルの削除もせず、かなりシンプルな内容に戻りましたし、ダイアログ表示と同時にYouTubeムービーの再生を開始する機能も復活。
Thanks!>edama2氏
| AppleScript名:YouTubeムービー再生実験 v2c.scpt |
| — – Created by: Takaaki Naganoya – Created on: 2025/11/13 – Modified by edama2 & Takaaki Naganoya on: 2025/11/13 — – Copyright © 2025 Piyomaru Software, All Rights Reserved — use AppleScript version "2.8" use framework "Cocoa" 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 "Simple YouTube Player" set viewSizeArray to {1280, 720} set vId to "GP_tVXTYdmY" set startSec to 32 set htmlString to "<body style=’background-color:red;overflow:hidden;margin:auto 0;padding:auto 0;’><div id=’ytplayer’></div> <script> var tag=document.createElement(’script’); tag.src=’https://www.youtube.com/player_api’; var firstScriptTag=document.getElementsByTagName(’script’)[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var player; function onYouTubePlayerAPIReady() {player = new YT.Player(’ytplayer’,{height:’100%’,width:’100%’,videoId:’" & vId & "’,playerVars:{autoplay:1,mute:0,start:" & (startSec as string) & "}});} </script>" set paramObj to {myMessage:mainMessage, mySubMessage:subMessage, viewSize:viewSizeArray, myHTML:htmlString} –my browseURLContents:paramObj–For Debug with Script Editor my performSelectorOnMainThread:"browseURLContents:" withObject:(paramObj) waitUntilDone:true — WKWebView表示用ハンドラ on browseURLContents:paramObj set webSize to viewSize of paramObj copy webSize to {aWidth, aHeight} set htmlString to myHTML of paramObj set idConf to current application’s WKWebViewConfiguration’s new() set aWebView to current application’s WKWebView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight)) configuration:idConf set baseURL to (current application’s NSURL’s URLWithString:"https://www.google.com/") aWebView’s loadHTMLString:htmlString baseURL:baseURL — アラートに埋め込む set theAlert to current application’s NSAlert’s new() 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の後始末 aWebView’s loadHTMLString:"" baseURL:(missing value) end browseURLContents: |
(Visited 10 times, 4 visits today)




