Markdown書類からリンクしているローカルの画像(相対パス表記)へのリンクを書き換えるAppleScriptです。
MacDownが画像リンクの管理などは一切してくれないので、自前で(AppleScriptで)書き換えを行なっています。
ルートフォルダはフォルダ名が「–」ではじまるようにルールを(勝手に)決めており、各Markdown書類フォルダをさらに細分化した場合には、Markdown書類から画像フォルダへの相対パス指定が合わなくなってしまいます。

そこで、画像フォルダを求めてMarkdown書類の画像リンクを再計算して書き換えてみました。画像自体をルートフォルダからSpotlightで検索するようにしてもよいのですが、今回はとりあえずルールを自分で決めて自分で守っているので、このように処理してみました。
ただ、いまだにリンク画像のパスを手で書かされる(記述自体はAppleScriptでその場で計算しているので完全手書きではないですが)のには、いささかMarkdownの仕様の素朴さに呆れてしまうところです、、、、
| AppleScript名:Markdownのimglinkタグ行のリンク書き換え |
| — Created 2017-01-26 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use mdLib : script "Metadata Lib" version "2.0.0" –https://www.macosxautomation.com/applescript/apps/ property NSArray : a reference to current application’s NSArray property NSString : a reference to current application’s NSString property NSScanner : a reference to current application’s NSScanner property NSPredicate : a reference to current application’s NSPredicate property NSDictionary : a reference to current application’s NSDictionary property NSMutableArray : a reference to current application’s NSMutableArray property NSDataDetector : a reference to current application’s NSDataDetector property NSAttributedString : a reference to current application’s NSAttributedString property NSUTF8StringEncoding : a reference to current application’s NSUTF8StringEncoding property NSTextCheckingTypeLink : a reference to current application’s NSTextCheckingTypeLink set origPath to POSIX path of (choose folder with prompt "Markdown書類が入っているフォルダを選択") set savePath to POSIX path of (choose folder with prompt "画像フォルダを選択") set tmp2 to NSString’s stringWithString:savePath set tmp3 to (tmp2’s lastPathComponent()) as string –Spotlightで指定フォルダ以下のMarkdown書類を検索 set aRes to mdLib’s searchFolders:{origPath} searchString:("kMDItemKind == %@ ") searchArgs:{"Markdown Document"} repeat with i in aRes –テキストエンコーディングをUTF-8でMarkDown書類テキスト読み込み set aText to (NSString’s stringWithContentsOfFile:(i) encoding:(NSUTF8StringEncoding) |error|:(missing value)) as string –Markdown記法の画像タグが入っている場合のみ処理 set repLinkURLs to {} –パス置換対象リスト(oldPath, newPathでペア) set aFreq to retFrequency(aText, "![") of me if aFreq is not equal to 0 then set bList to parseStringParagraphs(NSString’s stringWithString:aText) of me set aPredicates to NSPredicate’s predicateWithFormat_("SELF BEGINSWITH[cd] %@", "








