| 001 | #!/usr/bin/env osascript |
| 002 | #coding: utf-8 |
| 003 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7-- |
| 004 | (* |
| 005 | noteのインラインフレームHTMLリンクの生成 |
| 006 | 単機能 |
| 007 |
|
| 008 |
|
| 009 | com.cocolog-nifty.quicktimer.icefloe *) |
| 010 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7-- |
| 011 | use AppleScript version "2.8" |
| 012 | use framework "Foundation" |
| 013 | use framework "AppKit" |
| 014 | use framework "UniformTypeIdentifiers" |
| 015 | use scripting additions |
| 016 | property refMe : a reference to current application |
| 017 |
|
| 018 | ############################## |
| 019 | # 設定項目幅PX |
| 020 | set strWpx to ("680") as text |
| 021 | ############################## |
| 022 | # クリップボードの中身取り出し |
| 023 | set appPasteboard to refMe's NSPasteboard's generalPasteboard() |
| 024 | set ocidPastBoardTypeArray to appPasteboard's types() |
| 025 | set boolContain to ocidPastBoardTypeArray's containsObject:(refMe's NSStringPboardType) |
| 026 | if (boolContain as boolean) is true then |
| 027 | set ocidString to appPasteboard's stringForType:(refMe's NSStringPboardType) |
| 028 | set strDefaultAnswer to ocidString as text |
| 029 | else |
| 030 | set strDefaultAnswer to ("URLを入力") as text |
| 031 | end if |
| 032 | ############################## |
| 033 | # ダイアログ |
| 034 | set strIconFilePath to ("/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/GenericNetworkIcon.icns") as text |
| 035 | set aliasIconFilePath to (POSIX file strIconFilePath) as alias |
| 036 | set strMsg to ("URLを入力してください" & return & "iframeに変換します") as text |
| 037 | set strTitle to ("入力してください") as text |
| 038 | set strOK to ("OK") as text |
| 039 | set strCancel to ("キャンセル") as text |
| 040 | set listBotton to {strOK, strCancel} as list |
| 041 | try |
| 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 |
| 046 | on error strErrMsg number numErrNo |
| 047 | log strErrMsg & numErrNo |
| 048 | return false |
| 049 | end try |
| 050 | try |
| 051 | tell application "System Events" to quit |
| 052 | end try |
| 053 | set strResponseButton to (button returned of recordResult) as text |
| 054 | set boolGaveUp to (gave up of recordResult) as boolean |
| 055 | if boolGaveUp is true then |
| 056 | log "時間切れ" |
| 057 | return false |
| 058 | else if strResponseButton is strOK then |
| 059 | set strReturnedText to (text returned of recordResult) as text |
| 060 | end if |
| 061 | ############################## |
| 062 | set strLF to ("" & linefeed & "") as text |
| 063 | set strCR to ("" & return & "") as text |
| 064 | set strCRLF to ("" & return & linefeed & "") as text |
| 065 | set strSpace to ("" & space & "") as text |
| 066 | set strTab to ("" & tab & "") as text |
| 067 | #戻り値整形 |
| 068 | set ocidOrgStrings to (refMe's NSString's stringWithString:(strReturnedText)) |
| 069 | set ocidReplacedStrings to (ocidOrgStrings's stringByReplacingOccurrencesOfString:(strCRLF) withString:(strLF)) |
| 070 | set ocidReplacedStrings to (ocidReplacedStrings's stringByReplacingOccurrencesOfString:(strCR) withString:(strLF)) |
| 071 | set ocidURLString to (ocidReplacedStrings's stringByReplacingOccurrencesOfString:(strLF & strLF) withString:(strLF)) |
| 072 | #URL判定 |
| 073 | #URLか?の判定 |
| 074 | set numLength to ocidURLString's |length|() |
| 075 | set ocidRange to refMe's NSRange's NSMakeRange(0, numLength) |
| 076 | set listResponse to refMe's NSDataDetector's dataDetectorWithTypes:(refMe's NSTextCheckingTypeLink) |error|:(reference) |
| 077 | set appDetector to (first item of listResponse) |
| 078 | set ocidMatch to appDetector's matchesInString:(ocidURLString) options:(refMe's NSMatchingAnchored) range:(ocidRange) |
| 079 | set numCntMactch to ocidMatch's |count|() as integer |
| 080 | if numCntMactch = 0 then |
| 081 | log "B:URLの形式に誤りがあります " |
| 082 | return false |
| 083 | else 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 |
| 088 | else |
| 089 | log "C:URLを入力してください" |
| 090 | return false |
| 091 | end if |
| 092 | set 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 | #ダイアログ |
| 096 | set strSaveFile to ("ファイルに保存") as text |
| 097 | set strCopy to ("クリップボードにコピー") as text |
| 098 | set strCancel to ("終了") as text |
| 099 | set listButtonName to {strSaveFile, strCopy, strCancel} as list |
| 100 | set strMsg to ("type identifier 戻り値です") as text |
| 101 | set strTitle to ("uniform type identifier") as text |
| 102 | set strDefaultAnswer to strHTML as text |
| 103 | try |
| 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 |
| 108 | on error strErrMsg number numErrNo |
| 109 | log strErrMsg & numErrNo |
| 110 | return false |
| 111 | end try |
| 112 | set strButtonReturned to (button returned of recordResult) as text |
| 113 | set strText to (text returned of recordResult) as text |
| 114 | set ocidText to (refMe's NSString's stringWithString:(strText)) |
| 115 | ############################## |
| 116 | #クリップボードコピー |
| 117 | if 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 |
| 122 | end if |
| 123 | ############################## |
| 124 | #ファイル保存 |
| 125 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 126 | if 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) |
| 160 | else |
| 161 | log (text returned of recordResult) as text |
| 162 | end if |
| 163 | return true |