20260325

noteのインラインフレームのHTMLリンクを作成する


noteのインラインフレームのHTMLリンクを作成する

ダウンロードはこちら
NOTE記事一覧ですnote.com

【スクリプトエディタで開く】 |

NoteのiframeリンクHTML生成.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
004(*
005noteのインラインフレームHTMLリンクの生成
006単機能
007
008
009com.cocolog-nifty.quicktimer.icefloe *)
010----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
011use AppleScript version "2.8"
012use framework "Foundation"
013use framework "AppKit"
014use framework "UniformTypeIdentifiers"
015use scripting additions
016property refMe : a reference to current application
017
018##############################
019#   設定項目幅PX
020set strWpx to ("680") as text
021##############################
022#   クリップボードの中身取り出し
023set appPasteboard to refMe's NSPasteboard's generalPasteboard()
024set ocidPastBoardTypeArray to appPasteboard's types()
025set boolContain to ocidPastBoardTypeArray's containsObject:(refMe's NSStringPboardType)
026if (boolContain as boolean) is true then
027   set ocidString to appPasteboard's stringForType:(refMe's NSStringPboardType)
028   set strDefaultAnswer to ocidString as text
029else
030   set strDefaultAnswer to ("URLを入力") as text
031end if
032##############################
033#   ダイアログ
034set strIconFilePath to ("/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/GenericNetworkIcon.icns") as text
035set aliasIconFilePath to (POSIX file strIconFilePath) as alias
036set strMsg to ("URLを入力してください" & return & "iframeに変換します") as text
037set strTitle to ("入力してください") as text
038set strOK to ("OK") as text
039set strCancel to ("キャンセル") as text
040set listBotton to {strOK, strCancel} as list
041try
042   tell application "System Events"
043      activate
044      set recordResult to (display dialog strMsg with title strTitle default answer strDefaultAnswer buttons listBotton default button strOK cancel button strCancel with icon aliasIconFilePath giving up after 20 without hidden answer) as record
045   end tell
046on error strErrMsg number numErrNo
047   log strErrMsg & numErrNo
048   return false
049end try
050try
051   tell application "System Events" to quit
052end try
053set strResponseButton to (button returned of recordResult) as text
054set boolGaveUp to (gave up of recordResult) as boolean
055if boolGaveUp is true then
056   log "時間切れ"
057   return false
058else if strResponseButton is strOK then
059   set strReturnedText to (text returned of recordResult) as text
060end if
061##############################
062set strLF to ("" & linefeed & "") as text
063set strCR to ("" & return & "") as text
064set strCRLF to ("" & return & linefeed & "") as text
065set strSpace to ("" & space & "") as text
066set strTab to ("" & tab & "") as text
067#戻り値整形
068set ocidOrgStrings to (refMe's NSString's stringWithString:(strReturnedText))
069set ocidReplacedStrings to (ocidOrgStrings's stringByReplacingOccurrencesOfString:(strCRLF) withString:(strLF))
070set ocidReplacedStrings to (ocidReplacedStrings's stringByReplacingOccurrencesOfString:(strCR) withString:(strLF))
071set ocidURLString to (ocidReplacedStrings's stringByReplacingOccurrencesOfString:(strLF & strLF) withString:(strLF))
072#URL判定
073#URLか?の判定
074set numLength to ocidURLString's |length|()
075set ocidRange to refMe's NSRange's NSMakeRange(0, numLength)
076set listResponse to refMe's NSDataDetector's dataDetectorWithTypes:(refMe's NSTextCheckingTypeLink) |error|:(reference)
077set appDetector to (first item of listResponse)
078set ocidMatch to appDetector's matchesInString:(ocidURLString) options:(refMe's NSMatchingAnchored) range:(ocidRange)
079set numCntMactch to ocidMatch's |count|() as integer
080if numCntMactch = 0 then
081   log "B:URLの形式に誤りがあります "
082   return false
083else if numCntMactch = 1 then
084   set ocidItemMatch to ocidMatch's firstObject()
085   set ocidMatchURL to ocidItemMatch's |URL|()
086   set ocidID to ocidMatchURL's lastPathComponent()
087   set strURL to ocidMatchURL's absoluteString() as text
088else
089   log "C:URLを入力してください"
090   return false
091end if
092set strHTML to ("<div style=\"clear: both;text-align: center;\"><iframe class=\"note-embed\" data-ready=\"true\" data-src=\"https://note.com/embed/notes/" & ocidID & "\" height=\"240px\" loading=\"lazy\" scrolling=\"no\" src=\"https://note.com/embed/notes/" & ocidID & "\" style=\"background-repeat: no-repeat; border-style: initial; border-width: 0px; box-sizing: inherit; display: block; margin: 10px 0px; max-width: none; padding: 0px; position: static; vertical-align: initial; visibility: visible; width: " & strWpx & "px;\"></iframe></div>") as text
093
094##############################
095#ダイアログ
096set strSaveFile to ("ファイルに保存") as text
097set strCopy to ("クリップボードにコピー") as text
098set strCancel to ("終了") as text
099set listButtonName to {strSaveFile, strCopy, strCancel} as list
100set strMsg to ("type identifier 戻り値です") as text
101set strTitle to ("uniform type identifier") as text
102set strDefaultAnswer to strHTML as text
103try
104   tell application "System Events"
105      activate
106      set recordResult to (display dialog strMsg with title strTitle default answer strDefaultAnswer buttons listButtonName default button strCopy cancel button strCancel giving up after 60 with icon aliasIconFilePath without hidden answer) as record
107   end tell
108on error strErrMsg number numErrNo
109   log strErrMsg & numErrNo
110   return false
111end try
112set strButtonReturned to (button returned of recordResult) as text
113set strText to (text returned of recordResult) as text
114set ocidText to (refMe's NSString's stringWithString:(strText))
115##############################
116#クリップボードコピー
117if strButtonReturned is strCopy then
118   set appPasteboard to refMe's NSPasteboard's generalPasteboard()
119   appPasteboard's clearContents()
120   set boolDone to appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
121   return boolDone
122end if
123##############################
124#ファイル保存
125set appFileManager to refMe's NSFileManager's defaultManager()
126if strButtonReturned is strSaveFile then
127   set strSaveFileName to ("" & ocidID & ".html") as text
128   set strTargetExtension to ("html") as text
129   set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
130   set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
131   set aliasDesktopDirPath to (ocidDesktopDirPathURL's absoluteURL()) as alias
132   #
133   set strMsg to ("テキスト・ファイルを保存") as text
134   set strPrompt to ("テキスト・ファイルに保存します") as text
135   try
136      tell application "SystemUIServer"
137         activate
138         set aliasFilePath to (choose file name strMsg with prompt strPrompt default location aliasDesktopDirPath default name strSaveFileName) as «class furl»
139      end tell
140   on error strErrMes number numErrNo
141      log strErrMes & numErrNo
142      return false
143   end try
144   set strFilePath to (POSIX path of aliasFilePath) as text
145   set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
146   set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
147   set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
148   set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
149   ###拡張子
150   set strExtension to (ocidFilePathURL's pathExtension()) as text
151   ###最後のアイテムがファイル名
152   set strFileName to (ocidFilePathURL's lastPathComponent()) as text
153   ###拡張子のつけ忘れ対策
154   if strFileName does not contain strTargetExtension then
155      set strFileName to (strFileName & "." & strTargetExtension) as text
156      set ocidFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:(strFileName)
157   end if
158   set ocidOutPutstring to (ocidText's stringByReplacingOccurrencesOfString:(strCR) withString:(strLF))
159   set listDone to ocidOutPutstring's writeToURL:(ocidFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
160else
161   log (text returned of recordResult) as text
162end if
163return true
AppleScriptで生成しました