Text MD

TSVタブ区切りのテキストをMDファイルの表にする


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003# com.cocolog-nifty.quicktimer.icefloe
004#
005----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
006use AppleScript version "2.8"
007use framework "Foundation"
008use framework "AppKit"
009use scripting additions
010property refMe : a reference to current application
011
012#################
013# 【1】入力ファイル
014#ダイアログ
015tell current application
016  set strName to name as text
017end tell
018#スクリプトメニューから実行したら
019if strName is "osascript" then
020  tell application "Finder" to activate
021else
022  tell current application to activate
023end if
024#デフォルトロケーション
025set appFileManager to refMe's NSFileManager's defaultManager()
026set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
027set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
028set aliasDesktopDirPath to (ocidDesktopDirPathURL's absoluteURL()) as alias
029#
030# TSV専用の場合 
031#set listUTI to {"public.tab-separated-values-text"}
032# テキストでもOKな場合
033set listUTI to {"public.tab-separated-values-text", "public.plain-text"}
034set strMes to ("TSVファイルを選んでください") as text
035set strPrompt to ("TSVファイルを選んでください") as text
036try
037  ### ファイル選択時
038  set aliasFilePath to (choose file strMes with prompt strPrompt default location aliasDesktopDirPath of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias
039on error
040  log "エラーしました"
041  return "エラーしました"
042end try
043if aliasFilePath is (missing value) then
044  return "選んでください"
045end if
046#入力パス
047set strFilePath to (POSIX path of aliasFilePath) as text
048set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
049set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
050set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
051#保存するタブ区切りテキストのパス
052set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension()
053set ocidFileName to ocidBaseFilePathURL's lastPathComponent()
054set ocidSaveFilePathURL to (ocidBaseFilePathURL's URLByAppendingPathExtension:("md"))
055#################
056### 【2】 ファイルのテキストを読み込み
057set listResponse to (refMe's NSString's alloc()'s initWithContentsOfURL:(ocidFilePathURL) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference))
058if (item 2 of listResponse) = (missing value) then
059  set ocidReadString to (item 1 of listResponse)
060else if (item 2 of listResponse) ≠ (missing value) then
061  set strErrorNO to (item 2 of listResponse)'s code() as text
062  set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text
063  refMe's NSLog("■:" & strErrorNO & strErrorMes)
064  return "エラーしました" & strErrorNO & strErrorMes
065end if
066#可変にして
067set ocidLineString to (refMe's NSMutableString's alloc()'s initWithCapacity:(0))
068(ocidLineString's setString:(ocidReadString))
069#改行をUNIXに強制
070set ocidLineStringLF to (ocidLineString's stringByReplacingOccurrencesOfString:("\r\n") withString:("\n"))
071set ocidLineString to (ocidLineStringLF's stringByReplacingOccurrencesOfString:("\r") withString:("\n"))
072#改行毎でリストにする
073set ocidCharSet to (refMe's NSCharacterSet's newlineCharacterSet)
074set ocidLineArray to (ocidLineString's componentsSeparatedByCharactersInSet:(ocidCharSet))
075set nunCntArray to (ocidLineArray's |count|()) as integer
076#################
077#【3】データをMDに整形
078#出力(MD)用のテキスト
079set ocidSaveString to (refMe's NSMutableString's alloc()'s initWithCapacity:(0))
080#表題を#で付与
081(ocidSaveString's appendString:("# 表のタイトル表のタイトル表のタイトル"))
082(ocidSaveString's appendString:("\n\n"))
083#行の数だけ繰り返し
084repeat with itemNo from 0 to (nunCntArray - 1) by 1
085  #行テキストを取り出して
086  set ocidLineText to (ocidLineArray's objectAtIndex:(itemNo))
087  #タブでリスト化する
088  set ocidLineTextArray to (ocidLineText's componentsSeparatedByString:("\t"))
089  #リスト化された行テキストを順番に
090  repeat with itemLineText in ocidLineTextArray
091    (ocidSaveString's appendString:("|"))
092    (ocidSaveString's appendString:(itemLineText))
093  end repeat
094  (ocidSaveString's appendString:("|"))
095  (ocidSaveString's appendString:("\n"))
096  #文字寄せ方向を付与(全部中央揃)
097  # 出力結果をデータの種類によって:を取れば良い
098  if itemNo = 0 then
099    set numCntLineTextArray to (ocidLineTextArray's |count|()) as integer
100    repeat numCntLineTextArray times
101      (ocidSaveString's appendString:("|"))
102      (ocidSaveString's appendString:(":---:"))
103    end repeat
104    (ocidSaveString's appendString:("|"))
105    (ocidSaveString's appendString:("\n"))
106  end if
107end repeat
108
109
110#################
111#保存
112set listDone to ocidSaveString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference)
113if (item 2 of listDone) ≠ (missing value) then
114  set strErrorNO to (item 2 of listDone)'s code() as text
115  set strErrorMes to (item 2 of listDone)'s localizedDescription() as text
116  refMe's NSLog("■:" & strErrorNO & strErrorMes)
117  return "エラーしました" & strErrorNO & strErrorMes
118end if
119
120
AppleScriptで生成しました

|

その他のカテゴリー

Accessibility Acrobat Acrobat 2020 Acrobat AddOn Acrobat Annotation Acrobat ARMDC Acrobat AV2 Acrobat BookMark Acrobat Classic Acrobat DC Acrobat Dialog Acrobat Distiller Acrobat Form Acrobat JS Acrobat Manifest Acrobat Menu Acrobat Open Acrobat Plugin Acrobat Preferences Acrobat Preflight Acrobat python Acrobat Reader Acrobat SCA Acrobat SCA Updater Acrobat Sequ Acrobat Sign Acrobat Stamps Acrobat Watermark Acrobat Windows Acrobat Windows Reader Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Mobileconfig Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin Support Admin System Information Admin Tools Admin Users Admin Volumes Adobe Adobe FDKO Adobe RemoteUpdateManager AppKit Apple AppleScript AppleScript do shell script AppleScript List AppleScript ObjC AppleScript Osax AppleScript PDF AppleScript Pictures AppleScript record AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript System Events AppleScript System Events Plist AppleScript Video Applications AppStore Archive Attributes Automator BackUp Barcode Barcode QR Barcode QR Decode Bash Basic Basic Path Bluetooth BOX Browser Calendar CD/DVD Choose Chrome CIImage CityCode CloudStorage Color com.apple.LaunchServices.OpenWith Console Contacts CotEditor CURL current application Date&Time delimiters Desktop Device Diff Disk Dock DropBox Droplet eMail Encode % Encode Decode Encode UTF8 Error EXIFData ffmpeg File Finder Firefox Folder FolderAction Fonts GIF github Guide HTML HTML Entity Icon Illustrator Image Events Image2PDF ImageOptim iPhone iWork Javascript Jedit Json Label Leading Zero List locationd LRC lsappinfo LSSharedFileList m3u8 Mail MakePDF Map Math Media Media AVAsset Media AVconvert Media AVFoundation Media AVURLAsset Media Movie Media Music Memo Messages Microsoft Microsoft Edge Microsoft Excel Mouse Music NetWork Notes NSArray NSArray Sort NSBezierPath NSBitmapImageRep NSBundle NSCFBoolean NSCharacterSet NSColor NSColorList NSData NSDecimalNumber NSDictionary NSError NSEvent NSFileAttributes NSFileManager NSFileManager enumeratorAtURL NSFont NSFontManager NSGraphicsContext NSImage NSIndex NSKeyedArchiver NSKeyedUnarchiver NSLocale NSMutableArray NSMutableDictionary NSMutableString NSNotFound NSNumber NSOpenPanel NSPasteboard NSpoint NSPredicate NSPrintOperation NSRange NSRect NSRegularExpression NSRunningApplication NSScreen NSSize NSString NSString stringByApplyingTransform NSStringCompareOptions NSTask NSTimeZone NSURL NSURL File NSURLBookmark NSURLComponents NSURLResourceKey NSURLSession NSUserDefaults NSUUID NSView NSWorkspace Numbers OAuth OneDrive PDF PDFAnnotation PDFAnnotationWidget PDFContext PDFDisplayBox PDFDocumentPermissions PDFImageRep PDFKit PDFnUP PDFOutline perl Photoshop PlistBuddy pluginkit postalcode PostScript prefPane Preview Python QuickLook QuickTime ReadMe Regular Expression Reminders ReName Repeat RTF Safari SaveFile ScreenCapture ScreenSaver SF Symbols character id SF Symbols Entity sips Skype Slack Sound Spotlight sqlite SRT StandardAdditions Swift System Settings TCC TemporaryItems Terminal Text Text CSV Text MD Text TSV TextEdit Tools Translate Trash Twitter Typography UI Unit Conversion UTType valueForKeyPath Video VisionKit Visual Studio Code Wacom webarchive webp Wifi Windows XML XML EPUB XML OPML XML Plist XML RSS XML savedSearch XML SVG XML TTML XML webloc XML XMP YouTube zoom