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

指定IDの戦場の絆のムービーをOffLiberty経由でローカルにダウンロード v5

# 電子書籍「AppleScript最新リファレンス」の補完のために追記した記事です

アーケードゲーム「戦場の絆」では、1日に2回までリプレイムービーをYouTubeにアップロードしてもらえるよう申請を行えるようになっています(公式Webサイトの有料会員契約を行うと1日に4回)。

ゲーム機(POD)内でYouTubeへのリプレイムービーアップロード申請を行うと、ゲームセンターのPODの付近に設置されているターミナル(リプレイムービーなどを大型モニタで表示している機械)でリプレイムービーにアクセスするための「リプレイID」を確認できます。

このリプレイIDを覚えておくなり……写真に撮って帰宅(すぐにはリプレイムービーはアップロードされないため)、リプレイムービー検索ページのフォームにこのリプレイIDを入れて検索ボタンを押すと、YouTube上のリプレイURL(短縮URL)が表示されます。

このURLに飛ぶと、リプレイムービーを見られるようになっています。

本AppleScriptでは、リプレイIDをリストで書いておくと、リプレイムービー検索ページのフォームにリプレイIDを自動入力して検索を行い、URLを取得してYouTubeでオープン。この状態でOffLibertyサイトにYouTubeのURLを指定してオープンすると、OffLibertyサイトを介してローカルにリプレイムービーがダウンロードされます。

実行のためには、GUI Scriptingをオンにしておく必要があります。次に、Safariで「開発」メニューをオンにして、「スマート検索フィールドからのJavaScriptを許可」「AppleEventからのJavaScriptを許可」をイネーブルにしておく必要があります(要・管理者パスワード)。

また、SafariのGUI Scripting的なHTML Viewへの参照を求める「safari_gui_ref_lib」を~/Library/Script Librariesフォルダにインストールしておく必要があります。

–> Download safari_gui_ref_lib (To ~/Library/Script Libraries/)

AppleScript名:指定IDの戦場の絆のムービーをOffLiberty経由でローカルにダウンロード v5
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use safari_gui_lib : script "safari_gui_ref_lib"

(*
set a to the clipboard
set aList to paragraphs of a
*)

set aList to {"211x3gk1", "7g2tf3gk"}

set erList to {}

set aaList to uniquify1DList(aList, true) of me –IDのユニーク化を追加

repeat with i in aaList
  
  
set j to contents of i
  
if j is not equal to "" then
    
    
–キーコードからURLへの変換を行う
    
set aURL to convKeyToURL(j) of me
    
    
if aURL is not equal to "" or aURL is not equal to false then
      if (aURL starts with "https://") or (aURL starts with "http://") then
        
        
–指定URLのYouTubeのムービーをダウンロードする
        
set aRes to downloadYoutubeMovieViaOffLiberty(aURL) of me
        
set the end of erList to aRes
        
      end if
    end if
    
  end if
end repeat

return erList –ダウンロード(エラー)結果

(*
–ダウンロードしたいYouTubeのムービーのURL
set aURL to "https://www.youtube.com/watch?t=261&v=k95yIQn2Sdg"
set aRes to downloadYoutubeMovieViaOffLiberty(aURL) of me
–> true (OK) / false (NG)
*)

–指定のURLのYouTubeのムービーをOffLiverty経由でローカルにダウンロード(2015/7/18 Version)
on downloadYoutubeMovieViaOffLiberty(aURL)
  
  
set groupList to {6, 3} –Click Link Group in OffLiberty Page
  
  
–OffLibertyの呼び出しブックマークレット
  
set jsText to "var title = window.location.href;
  if (title.indexOf(’http://offliberty.com’) == 0) { (function(){ location.href=’http://offliberty.com/#bookmarklet’; })(); }
  else { (function(){ window.open(’http://offliberty.com/#’+encodeURIComponent(location.href)); })(); }"

  
  
tell application "Safari"
    close every document
  end tell
  
  
  
tell application "Safari"
    make new document with properties {URL:aURL}
    
delay 1
    
do JavaScript jsText in document 1
    
activate
  end tell
  
  
tell current application
    delay 10
  end tell
  
  
–delay後に実行しないとHTML Viewへの参照が取得できなかった(要注意ポイント)
  
set aHTMLView to retSafariHTMLViewReference() of safari_gui_lib
  
  
tell application "Safari"
    activate
  end tell
  
  
  
set hitF to false
  
  
repeat with aCount from 1 to 1000
    
    
tell current application
      delay 5
    end tell
    
    
set aTitle to ""
    
repeat with ii in groupList
      
      
set jj to contents of ii
      
      
tell application "System Events"
        tell aHTMLView
          
          
tell group jj –1回変換したものはサイト側でキャッシュしているようなのでループ(6–>3)で対応
            tell UI element 1
              try
                set aTitle to title
              end try
            end tell
          end tell
          
          
if (jj = 3 and aTitle = "Right-click here and ’Save link as…’") then
            –オーディオファイルをダウンロードさせようとして、ビデオを直接ダウンロードさせない場合の対応
            
tell group 4
              click button 1 –"I want video file"ボタンをクリック
              
              
tell current application
                delay 5
              end tell
              
            end tell
          else if jj = 6 and aTitle = "Right-click here and ’Save link as…’" then
            set hitF to true
            
tell group jj
              tell UI element 1
                click –Download by clicking link
                
                
tell current application
                  delay 5
                end tell
                
                
return hitF
              end tell
            end tell
            
          end if
          
        end tell
      end tell
      
    end repeat
    
  end repeat
  
  
return hitF
  
end downloadYoutubeMovieViaOffLiberty

–Safariでオープン中のURLのローディング完了を待つ
on page_loaded(timeout_value)
  tell current application
    delay 1
  end tell
  
  
repeat with i from 1 to the timeout_value
    tell application "Safari"
      if (do JavaScript "document.readyState" in document 1) is "complete" then
        return true
      else if i is the timeout_value then
        return false
      else
        tell current application
          delay 1
        end tell
      end if
    end tell
  end repeat
  
return false
end page_loaded

–バージョン番号文字列からメジャーバージョンを取り出し数値として返す
on retMajorVersionNumber(a)
  
  
set aStr to current application’s NSString’s stringWithString:a
  
–> "10.0.1" (NSString)
  
  
set aRes to (aStr’s componentsSeparatedByString:".")
  
–> {"10","0","1"} (NSArray)
  
  
set bRes to aRes’s firstObject()
  
–> "10" (NSString)
  
  
set cRes to bRes’s integerValue()
  
–> 10
  
  
return cRes as integer
  
end retMajorVersionNumber

–Window 1が通常のWebページを表示しているかどうか判定
on detectSafariWindow1Status()
  
  
–最前面のWindowの表示状態を取得して「処理対象外」のケースを検出する
  
tell application "Safari"
    –Windowの枚数をかぞえる
    
set wCount to count every window
    
if wCount = 0 then return false –Windowなしの場合には処理対象外
    
    
tell window 1
      set aProp to properties
    end tell
    
  end tell
  
  
  
try
    –Window 1に該当するdocumentを取得する
    
set aDoc to document of aProp
    
    
tell application "Safari"
      tell aDoc
        set bProp to properties
      end tell
    end tell
    
  on error
    –エラーが発生するのは通常ページ「ではない」ものを表示しているため、と判断
    
return false –Displaying pages of iCloud connected devices
    
  end try
  
  
  
tell application "Safari"
    set aURL to (URL of bProp)
  end tell
  
  
  
–「お気に入り」か「Pages of iCloud connected devices」表示中なら
  
if aURL = "favorites://" or aURL = missing value then
    return false
  end if
  
  
  
return true
  
end detectSafariWindow1Status

on convKeyToURL(aKey)
  
  
tell application "Safari"
    set dCount to count every document
    
if dCount > 0 then
      set URL of document 1 to "http://gundam-kizuna.jp/detail/replay/search/index.php"
    else
      make new document with properties {URL:"http://gundam-kizuna.jp/detail/replay/search/index.php"}
    end if
  end tell
  
  
–ローディング待ち
  
set aRes to page_loaded(60) of me
  
if aRes = false then return aRes
  
—
  
  
–SafariのGUI Scripting的な観点からWebViewへの参照を取得する
  
set aHTMLView to retSafariHTMLViewReference() of safari_gui_lib
  
if aHTMLView = false then return
  
  
tell application "System Events"
    tell aHTMLView
      tell text field 1 of group 2
        set value to aKey
      end tell
    end tell
  end tell
  
  
tell current application to delay 1
  
  
tell application "System Events"
    tell aHTMLView
      –tell text field 1 of group 2
      
tell group 3
        click
      end tell
    end tell
  end tell
  
  
tell current application to delay 1
  
  
–結果待ち
  
repeat 100 times
    
    
–キー文字列による検索がエラーになったかを判定
    
tell application "System Events"
      tell aHTMLView
        tell group 4
          set bTitle to value of static text 1
          
if bTitle is in {"まだ動画が公開されていません。", "リプレイ動画検索に失敗しました。"} then return false
        end tell
      end tell
    end tell
    
    
–結果のURLを取り出す
    
tell application "System Events"
      tell aHTMLView
        tell group 4
          tell UI element 2
            –Link
            
set aTitle to title
          end tell
        end tell
      end tell
    end tell
    
    
    
if aTitle starts with "http://youtu.be/" then
      return aTitle
    end if
    
    
tell current application to delay 1
    
  end repeat
  
  
–結果が得られなかった場合
  
return ""
  
end convKeyToURL

–1D Listをユニーク化
on uniquify1DList(theList as list, aBool as boolean)
  set aArray to current application’s NSArray’s arrayWithArray:theList
  
set bArray to aArray’s valueForKeyPath:"@distinctUnionOfObjects.self"
  
set bList to bArray as list
  
return bList
end uniquify1DList

★Click Here to Open This Script 

(Visited 107 times, 1 visits today)

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

Google Search

Popular posts

  • 開発機としてM2 Mac miniが来たのでガチレビュー
  • macOS 15, Sequoia
  • 指定のWordファイルをPDFに書き出す
  • Pages本執筆中に、2つの書類モード切り替えに気がついた
  • Numbersで選択範囲のセルの前後の空白を削除
  • メキシカンハットの描画
  • Pixelmator Pro v3.6.4でAppleScriptからの操作時の挙動に違和感が
  • AdobeがInDesign v19.4からPOSIX pathを採用
  • AppleScriptによる並列処理
  • Safariで「プロファイル」機能を使うとAppleScriptの処理に影響
  • Cocoa Scripting Course 続刊計画
  • macOS 14.xでScript Menuの実行速度が大幅に下がるバグ
  • AppleScript入門③AppleScriptを使った「自動化」とは?
  • macOS 15でも変化したText to Speech環境
  • Keynote/Pagesで選択中の表カラムの幅を均等割
  • デフォルトインストールされたフォント名を取得するAppleScript
  • macOS 15 リモートApple Eventsにバグ?
  • AppleScript入門① AppleScriptってなんだろう?
  • macOS 14で変更になったOSバージョン取得APIの返り値
  • Keynoteで2階層のスライドのタイトルをまとめてテキスト化

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1391) 10.14savvy (587) 10.15savvy (438) 11.0savvy (283) 12.0savvy (212) 13.0savvy (194) 14.0savvy (147) 15.0savvy (132) CotEditor (66) Finder (51) iTunes (19) Keynote (117) 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 (55) 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
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • parallel processing
  • 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)
  • 未分類

アーカイブ

  • 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