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

カテゴリー: File path

path control×2を作成 v2

Posted on 5月 10, 2018 by Takaaki Naganoya

Windowを作成して、その上に2つのNSPathControlを作成し、ドラッグ&ドロップでパス情報を取得するAppleScriptです。

–> Watch Demo Movie

パス情報をドラッグ&ドロップで受け付けるNSPathControlはXcode上でAppleScriptのアプリケーションを作成する場合にはよく使うGUI部品ですが、通常の(Script Editor上の)AppleScriptではあまり使っていませんでした。

AppleScriptでは、choose fileやchoose folderコマンドを複数回実行して複数のパス情報を取得するのが「いつものやり方」ですが、複数のフォルダを指定する場合で人為的なミスが許されない場合には、指定されたパス情報をあらためてダイアログで表示するなどの処理を入れていました。

受け付けた2つのパス情報は、とくにファイルであってもフォルダであってもかまわないのですが、Cocoaで取得したフォルダのPOSIX path情報は末尾にスラッシュがついていないため、AppleScriptのPOSIX pathとして使用する場合には末尾にスラッシュを補う必要があります。

AppleScript側からハンドラを強制的にMain Threadで実行しているため、Command-Control-Rで実行させる必要はありません。その一方で、Script Menuから呼び出した場合にはドラッグ&ドロップを受け付けることや、ボタンのクリックの受信ができません。

AppleScript名:path control×2を作成 v2
— Created 2018-05-09 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

property windisp : false
property wController : false
property resList : {}

set aButtonMSG to "OK"
set aSliderValMSG to "処理対象フォルダと移動先の指定"

set paramList to {aButtonMSG, aSliderValMSG}

my performSelectorOnMainThread:"getPathControlValue:" withObject:paramList waitUntilDone:true
return my resList

on getPathControlValue:paramList
  copy paramList to {aButtonMSG, aSliderValMSG}
  
  
set timeOutSecs to 180 –タイムアウト時間
  
set (my windisp) to true — Window表示中フラグ
  
  
set aView to current application’s NSView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, 600, 120))
  
  
–Labelをつくる
  
set a1TF to current application’s NSTextField’s alloc()’s initWithFrame:(current application’s NSMakeRect(20, 110, 80, 20))
  
set a2TF to current application’s NSTextField’s alloc()’s initWithFrame:(current application’s NSMakeRect(20, 70, 80, 20))
  
a1TF’s setEditable:false
  
a2TF’s setEditable:false
  
a1TF’s setStringValue:"処理対象:"
  
a2TF’s setStringValue:"移動後 :"
  
a1TF’s setDrawsBackground:false
  
a2TF’s setDrawsBackground:false
  
a1TF’s setBordered:false
  
a2TF’s setBordered:false
  
  
–Ppopup Buttonをつくる
  
set aPathControl to current application’s NSPathControl’s alloc()’s initWithFrame:(current application’s NSMakeRect(80, 110, 500, 20))
  
set bPathControl to current application’s NSPathControl’s alloc()’s initWithFrame:(current application’s NSMakeRect(80, 70, 500, 20))
  
  
aPathControl’s setBackgroundColor:(current application’s NSColor’s cyanColor())
  
bPathControl’s setBackgroundColor:(current application’s NSColor’s yellowColor())
  
  
set aHome to current application’s |NSURL|’s fileURLWithPath:(current application’s NSHomeDirectory())
  
aPathControl’s setURL:aHome
  
bPathControl’s setURL:aHome
  
  
–Buttonをつくる
  
set bButton to (current application’s NSButton’s alloc()’s initWithFrame:(current application’s NSMakeRect(200, 10, 180, 40)))
  
bButton’s setButtonType:(current application’s NSMomentaryLightButton)
  
bButton’s setBezelStyle:(current application’s NSRoundedBezelStyle)
  
bButton’s setTitle:aButtonMSG
  
bButton’s setTarget:me
  
bButton’s setAction:("clicked:")
  
bButton’s setKeyEquivalent:(return)
  
  
aView’s addSubview:a1TF
  
aView’s addSubview:a2TF
  
  
aView’s addSubview:aPathControl
  
aView’s addSubview:bPathControl
  
aView’s addSubview:bButton
  
aView’s setNeedsDisplay:true
  
  
–NSWindowControllerを作ってみた
  
set aWin to (my makeWinWithView(aView, 600, 160, aSliderValMSG))
  
set wController to current application’s NSWindowController’s alloc()
  
wController’s initWithWindow:aWin
  
  
wController’s showWindow:me
  
  
set aCount to timeOutSecs * 10
  
  
set hitF to false
  
repeat aCount times
    if (my windisp) = false then
      set hitF to true
      
exit repeat
    end if
    
delay 0.1
    
set aCount to aCount – 1
  end repeat
  
  
my closeWin:aWin
  
  
if hitF = true then
    set s1Val to (aPathControl’s |URL|’s |path|()) as string
    
set s2Val to (bPathControl’s |URL|’s |path|()) as string
  else
    set {s1Val, s2Val} to {false, false}
  end if
  
  
set resList to {s1Val, s2Val}
  
end getPathControlValue:

on clicked:aSender
  set (my windisp) to false
end clicked:

–make Window for Display
on makeWinWithView(aView, aWinWidth, aWinHeight, aTitle)
  set aScreen to current application’s NSScreen’s mainScreen()
  
set aFrame to {{0, 0}, {aWinWidth, aWinHeight}}
  
set aBacking to current application’s NSTitledWindowMask
  
set aDefer to current application’s NSBackingStoreBuffered
  
  
— Window
  
set aWin to current application’s NSWindow’s alloc()
  (
aWin’s initWithContentRect:aFrame styleMask:aBacking backing:aDefer defer:false screen:aScreen)
  
  
aWin’s setTitle:aTitle
  
aWin’s setDelegate:me
  
aWin’s setDisplaysWhenScreenProfileChanges:true
  
aWin’s setHasShadow:true
  
aWin’s setIgnoresMouseEvents:false
  
aWin’s setLevel:(current application’s NSNormalWindowLevel)
  
aWin’s setOpaque:false
  
aWin’s setReleasedWhenClosed:true
  
aWin’s |center|()
  
aWin’s setContentView:aView
  
  
return aWin
end makeWinWithView

–close win
on closeWin:aWindow
  repeat with n from 10 to 1 by -1
    (aWindow’s setAlphaValue:n / 10)
    
delay 0.02
  end repeat
  
aWindow’s |close|()
end closeWin:

★Click Here to Open This Script 

Posted in File path GUI | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

Markdownのimglinkタグ行のリンク書き換え

Posted on 5月 3, 2018 by Takaaki Naganoya

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] %@", "![")
    
set cList to (bList’s filteredArrayUsingPredicate:aPredicates) as list
    
    
–画像ダウンロードおよび、ダウンロードずみ画像への相対パスの計算ループ
    
repeat with ii in cList
      set jj to contents of ii –画像リンクのMarkdownタグ行
      
set tmpLinkPath to parseStrFromTo(jj, "(", ")") of me
      
      
set aStr to (NSString’s stringWithString:((contents of i) as string))
      
set aList to (aStr’s stringByDeletingLastPathComponent’s pathComponents()) as list of string or string
      
      
set aLen to length of aList
      
      
–リンク画像の相対パスを絶対パスに変換する
      
set rStr to (NSString’s stringWithString:tmpLinkPath)
      
set r2Str to rStr’s stringByDeletingLastPathComponent’s lastPathComponent() –フォルダ名(="9999_images")
      
set r3Str to (rStr’s lastPathComponent()) as string –ファイル名(="fake_scriptable.png")
      
      
–書籍のルートフォルダを求め、その直下にある画像フォルダを名指しで指定
      
repeat with i2 from aLen to 0 by -1
        set j to contents of (item i2 of aList)
        
if j begins with "–" then
          exit repeat
        end if
      end repeat
      
      
set f1Path to (items 1 thru i2 of aList) & r2Str & r3Str
      
set f2Path to (NSString’s pathWithComponents:f1Path) as string
      
      
–MarkDown書類と移動先の画像フォルダ中の画像の相対パスを求める
      
set newRelPath to calcRelativePathFromTwoAbsolutePaths(i, f2Path) of me
      
      
set the end of repLinkURLs to {tmpLinkPath, newRelPath}
      
    end repeat
    
    
–リンク書き換え
    
copy aText to bText
    
repeat with ii in repLinkURLs
      copy ii to {oldPath, newPath}
      
set bText to repChar(bText, oldPath, newPath) of me
    end repeat
    
    
–もともとのパスにMarkdown書類を上書き保存
    
set writeString to (NSString’s stringWithString:bText)
    
set ssRes to (writeString’s writeToFile:i atomically:true encoding:(NSUTF8StringEncoding) |error|:(missing value))
    
  end if
end repeat

–指定文字列内の指定キーワードの出現回数を取得する
on retFrequency(origText, aKeyText)
  set aRes to parseByDelim(origText, aKeyText) of me
  
return ((count every item of aRes) – 1)
end retFrequency

on parseByDelim(aData, aDelim)
  set curDelim to AppleScript’s text item delimiters
  
set AppleScript’s text item delimiters to aDelim
  
set dList to text items of aData
  
set AppleScript’s text item delimiters to curDelim
  
return dList
end parseByDelim

–テキストを行ごとにparseしてNSArrayに
on parseStringParagraphs(anNSString)
  set anArray to NSMutableArray’s alloc()’s init()
  
set aRange to current application’s NSMakeRange(0, anNSString’s |length|())
  
  
repeat while aRange’s |length|() > 0
    set subRange to anNSString’s lineRangeForRange:(current application’s NSMakeRange(aRange’s location(), 0))
    
    
–行が改行コードまで取得されるので、改行コードを除外するように微調整
    
copy subRange to tmpRange
    
set tmpRange’s |length| to ((subRange’s |length|()) – 1) –微調整
    
set aLine to anNSString’s substringWithRange:tmpRange
    
anArray’s addObject:aLine
    
    
set aRange’s location to (current application’s NSMaxRange(subRange))
    
set aRange’s |length| to ((aRange’s |length|()) – (subRange’s |length|()))
  end repeat
  
  
return anArray
end parseStringParagraphs

on repChar(origText, targStr, repStr)
  set {txdl, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, targStr}
  
set temp to text items of origText
  
set AppleScript’s text item delimiters to repStr
  
set res to temp as text
  
set AppleScript’s text item delimiters to txdl
  
return res
end repChar

on parseStrFromTo(aParamStr, fromStr, toStr)
  set theScanner to NSScanner’s scannerWithString:aParamStr
  
set anArray to NSMutableArray’s array()
  
  
repeat until (theScanner’s isAtEnd as boolean)
    — terminate check, return the result (aDict) to caller
    
set {theResult, theKey} to theScanner’s scanUpToString:fromStr intoString:(reference)
    
    
— skip over separator
    
theScanner’s scanString:fromStr intoString:(missing value)
    
set {theResult, theValue} to theScanner’s scanUpToString:toStr intoString:(reference)
    
if theValue is missing value then set theValue to "" –>追加
    
    
— skip over separator
    
theScanner’s scanString:toStr intoString:(missing value)
    
    
anArray’s addObject:theValue
  end repeat
  
  
if (anArray’s |count|()) as integer = 1 then
    return theValue as list of string or string
  else
    return anArray as list
  end if
end parseStrFromTo

–2つの絶対パス間の相対パスを求める
on calcRelativePathFromTwoAbsolutePaths(aPOSIXfile as string, bPOSIXfile as string)
  set aStr to NSString’s stringWithString:aPOSIXfile
  
set bStr to NSString’s stringWithString:bPOSIXfile
  
  
set aList to aStr’s pathComponents() as list
  
set bList to bStr’s pathComponents() as list
  
  
set aLen to length of aList
  
set bLen to length of bList
  
  
if aLen ≥ bLen then
    copy aLen to aMax
  else
    copy bLen to aMax
  end if
  
  
repeat with i from 1 to aMax
    set aTmp to contents of item i of aList
    
set bTmp to contents of item i of bList
    
    
if aTmp is not equal to bTmp then
      exit repeat
    end if
  end repeat
  
  
set bbList to items i thru -1 of bList
  
set aaItem to (length of aList) – i
  
  
set tmpStr to {}
  
repeat with ii from 1 to aaItem
    set the end of tmpStr to ".."
  end repeat
  
  
set allRes to NSString’s pathWithComponents:(tmpStr & bbList)
  
return allRes as text
end calcRelativePathFromTwoAbsolutePaths

★Click Here to Open This Script 

Posted in file File path Markdown Spotlight Text | Tagged 10.11savvy 10.12savvy 10.13savvy MacDown | Leave a comment

file URL文字列からPOSIX pathに変換 v2

Posted on 3月 31, 2018 by Takaaki Naganoya

“file://”ではじまるfile URL文字列からPOSIX pathに変換するAppleScriptの改修版です。

たまたまScripting Bridge経由でFinder上の選択中のファイル一覧を取得したら、”file://”ではじまる文字列が返ってきて、Cocoaに変換する機能がないか調べたものの….ない。

・・・と思ったら「あるよ」というのを教えてもらいました。

下調べして「path()」でNSURLから変換できるというのは調査してあったんですが、試行錯誤する中でうまくいかなかった記憶が…..結果的には教えていただいた方法で無事変換できた次第です。

AppleScript名:file URL文字列からPOSIX pathに変換 v2
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

set aStr to "file:///Users/me/Desktop/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202018-03-12%2015.22.44.png"

set aURL to (current application’s |NSURL|’s URLWithString:aStr)
set aPOSIX to aURL’s |path|() as string

★Click Here to Open This Script 

Posted in File path Text URL | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

file URL文字列からPOSIX pathに変換

Posted on 3月 29, 2018 by Takaaki Naganoya

“file://”ではじまるfile URL文字列からPOSIX pathに変換するAppleScriptです。

たまたまScripting Bridge経由でFinder上の選択中のファイル一覧を取得したら、”file://”ではじまる文字列が返ってきて、Cocoaに変換する機能がないか調べたものの….ない。

というわけで、とりあえず作ってみました。

AppleScript名:file URL文字列からPOSIX pathに変換
— Created 2018-03-29 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aStr to "file:///Users/me/Desktop/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202018-03-12%2015.22.44.png"

set bStr to convfileURLStringtoPOSIXpath(aStr) of me
–>  "/Users/me/Desktop/スクリーンショット 2018-03-12 15.22.44.png"

on convfileURLStringtoPOSIXpath(aURLencodedStr)
  set aStr to current application’s NSString’s stringWithString:aURLencodedStr
  
set aDecoded to (aStr’s stringByRemovingPercentEncoding()) as string
  
if aDecoded begins with "file:///" then
    return text (length of "file:///") thru -1 of aDecoded
  else
    return aDecoded
  end if
end convfileURLStringtoPOSIXpath

★Click Here to Open This Script 

Posted in File path Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

Finderの最前面のWindowで表示中のフォルダをTerminalでオープン

Posted on 2月 27, 2018 by Takaaki Naganoya

Finderの最前面のWindowで表示中のフォルダをTerminalでオープンするAppleScriptです。

AppleScript名:Finderの最前面のWindowで表示中のフォルダをTerminalでオープン
tell application "Finder"
  set wCount to count every window
  
if wCount = 0 then return
  
  
tell front window
    set aTarg to target as alias
  end tell
end tell

set targPOSIX to quoted form of (POSIX path of aTarg)
set aCom to "cd " & targPOSIX

tell application "Terminal"
  set tCount to count (every window whose visible is true)
  
if tCount = 0 then
    do script aCom
  else
    do script aCom in front window
  end if
end tell

★Click Here to Open This Script 

Posted in File path | Tagged 10.11savvy 10.12savvy 10.13savvy Finder Terminal | Leave a comment

POSIX pathからAliasへの戻し方(解説つき)

Posted on 2月 19, 2018 by Takaaki Naganoya
AppleScript名:POSIX pathからAliasへの戻し方(解説つき)
set a to choose folder
log {"alias", a}
–> (*alias, alias Cherry:Users:me:Desktop:*)

set aP to POSIX path of a
log {"POSIX path", aP}
–> (*POSIX path, /Users/me/Desktop/*)–shell commandに渡すときにはパスにquoted form ofを付ける必要アリ。Cocoaに渡すときにはquoted form ofは不要

set aF to POSIX file aP
log {"file", aF}
–> (*file, file Cherry:Users:me:Desktop:*)

set anAlias to aF as alias
log {"alias", anAlias}
–> (*alias, alias Cherry:Users:me:Desktop:*)

★Click Here to Open This Script 

Posted in File path | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

ファイルパスの階層とリストとの相互変換

Posted on 2月 19, 2018 by Takaaki Naganoya
AppleScript名:ファイルパスの階層とリストとの相互変換
— Created 2016-11-07 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aStr to current application’s NSString’s stringWithString:"/Users/maro/Desktop/aTEST.scpt"
set aList to aStr’s pathComponents() as list
–>  {​​​​​"/", ​​​​​"Users", ​​​​​"maro", ​​​​​"Desktop", ​​​​​"aTEST.scpt"​​​}

set bList to {"/", "Users", "maro", "Desktop", "aTEST.scpt"}
set bStr to (current application’s NSString’s pathWithComponents:bList) as string
–>  "/Users/maro/Desktop/aTEST.scpt"

★Click Here to Open This Script 

Posted in File path list | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

指定のパスのファイル名をUUIDにして拡張子を付け替える

Posted on 2月 19, 2018 by Takaaki Naganoya
AppleScript名:指定のパスのファイル名をUUIDにして拡張子を付け替える
— Created 2017-09-13 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

property NSString : a reference to current application’s NSString
property NSUUID : a reference to current application’s NSUUID

set aFile to POSIX path of (choose file of type {"public.html"})
set aRes to changeFileNameWithUUID(aFile, "pdf") of me

on changeFileNameWithUUID(aFile, newExt)
  set thePath to NSString’s stringWithString:aFile
  
set path1 to thePath’s stringByDeletingLastPathComponent()
  
set theName to NSUUID’s UUID()’s UUIDString()
  
set path2 to (path1’s stringByAppendingPathComponent:theName)’s stringByAppendingPathExtension:newExt
  
return path2
end changeFileNameWithUUID

★Click Here to Open This Script 

Posted in File path | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

POSIX pathからファイル名と親フォルダを抽出

Posted on 2月 19, 2018 by Takaaki Naganoya
AppleScript名:POSIX pathからファイル名と親フォルダを抽出
— Created 2016-05-25 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set targPath to "/20160524/IMG_1198.JPG"

set aPath to current application’s NSString’s stringWithString:targPath
set fileName to (aPath’s lastPathComponent()) as string
–>  "IMG_1198.JPG"
set parentFol to (aPath’s stringByDeletingLastPathComponent()) as string
–>  "/20160524"

★Click Here to Open This Script 

Posted in File path | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

指定ファイルの拡張子を取得する

Posted on 2月 19, 2018 by Takaaki Naganoya

指定ファイルの拡張子を取得するAppleScriptです。

純粋にパス文字列から拡張子を取得する方法(テキストを逆順に変換してピリオドに遭遇するところまでを拡張子とみなす)などもありますが、それは当時の環境(AppleScript Studio)でエラーの出にくい処理方法を選択したものでした。

FinderやSystem Eventsの機能を用いて拡張子を取得するのが一番手軽ではあるのですが、macOS 10.14以降の環境ではこの程度でもアプリケーションの機能を呼び出すと認証ダイアログが(初回のみですが)表示されます。Cocoaの機能呼び出しが手軽にできるので、些細な処理でもよく呼び出して使っています(個人的に)。

Mac App Storeに出すアプリケーションの内部で、ファイルの拡張子を求めるためだけにFinderやSystem Eventsのコントロールを行おうとするのはリジェクトの原因になります。そういう場合には純粋に文字列処理するか、Cocoaの機能を用いることになるでしょう。

AppleScript名:指定ファイルの拡張子を取得する
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set a to choose file
set aPath to POSIX path of a
set pathString to current application’s NSString’s stringWithString:aPath
set newPath to (pathString’s pathExtension()) as string
–>  "jpg"

★Click Here to Open This Script 

AppleScript名:ファイルから拡張子を取得(Finder)
set aFile to choose file

tell application "Finder"
  set aExt to name extension of aFile
end tell

★Click Here to Open This Script 

AppleScript名:ファイルから拡張子を取得(System Events)
set aFile to choose file

tell application "System Events"
  set aExt to name extension of aFile
end tell

★Click Here to Open This Script 

Posted in File path | Tagged 10.11savvy 10.12savvy 10.13savvy Finder System Events | Leave a comment

指定ファイルのフルパスから拡張子を削除した文字列を取得する

Posted on 2月 19, 2018 by Takaaki Naganoya
AppleScript名:指定ファイルのフルパスから拡張子を削除した文字列を取得する
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set a to choose file
set aPath to POSIX path of a
set pathString to current application’s NSString’s stringWithString:aPath
set newPath to pathString’s stringByDeletingPathExtension()
–>  (NSString) "/Users/me/Desktop/aTest"

★Click Here to Open This Script 

Posted in File path | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

ファイル名の文字列から拡張子を削る

Posted on 2月 19, 2018 by Takaaki Naganoya
AppleScript名:ファイル名の文字列から拡張子を削る
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aPath to "aTest.test"
set pathString to current application’s NSString’s stringWithString:aPath
set newPath to pathString’s stringByDeletingPathExtension() as string
–>  "aTest"

★Click Here to Open This Script 

Posted in File path | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

POSIX Pathから拡張子を外して、別の拡張子に付け替える

Posted on 2月 19, 2018 by Takaaki Naganoya
AppleScript名:POSIX Pathから拡張子を外して、別の拡張子に付け替える
— Created 2017-11-05 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aPath to "/Users/me/Desktop/test2.scpt"

set pathString to current application’s NSString’s stringWithString:aPath
set newPath to ((pathString’s stringByDeletingPathExtension())’s stringByAppendingPathExtension:"scptd") as string
–>  "/Users/me/Desktop/test2.scptd"

★Click Here to Open This Script 

Posted in File path | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

ファイル名から拡張子を外して、別の拡張子に付け替える

Posted on 2月 19, 2018 by Takaaki Naganoya
AppleScript名:ファイル名から拡張子を外して、別の拡張子に付け替える
— Created 2018-01-29 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set origName to "6020 Touch BarとAppleScript.md"
set newName to repFileNameExtension(origName, ".pdf") of me
–>  "6020 Touch BarとAppleScript.pdf"

set origName to "6020 Touch BarとAppleScript"
set newName to repFileNameExtension(origName, ".pdf") of me
–>  "6020 Touch BarとAppleScript.pdf"

on repFileNameExtension(origName, newExt)
  set aName to current application’s NSString’s stringWithString:origName
  
set theExtension to aName’s pathExtension()
  
if (theExtension as string) is not equal to "" then
    set thePathNoExt to aName’s stringByDeletingPathExtension()
    
set newName to (thePathNoExt’s stringByAppendingString:newExt)
  else
    set newName to (aName’s stringByAppendingString:newExt)
  end if
  
  return newName as string
end repFileNameExtension

★Click Here to Open This Script 

Posted in File path | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

Homeフォルダへのパスを求める1

Posted on 2月 19, 2018 by Takaaki Naganoya
AppleScript名:Homeフォルダへのパスを求める1
— Created 2016-02-19 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aDesktopPath to (current application’s NSProcessInfo’s processInfo()’s environment()’s objectForKey:"HOME") as string
–>  "/Users/me"

★Click Here to Open This Script 

Posted in File path | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

Homeフォルダへのパスを求める2

Posted on 2月 19, 2018 by Takaaki Naganoya
AppleScript名:Homeフォルダへのパスを求める2
— Created 2016-02-19 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set homeDir to (current application’s NSHomeDirectory()) as string
–>  "/Users/me"

★Click Here to Open This Script 

Posted in File path | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

path toの高度な機能(アプリケーションへのパス)

Posted on 2月 19, 2018 by Takaaki Naganoya
AppleScript名:path toの高度な機能(アプリケーションへのパス)
–現在最前面になっているアプリケーション
set bPath to path to frontmost application
–> alias "Macintosh HD:Applications:Utilities:Script Editor.app:"

–現在対象になっているアプリケーション
set aPath to path to current application
–> alias "Macintosh HD:Applications:Utilities:Script Editor.app:"

–アプリケーション名で指定
set cPath to path to application "Calendar"
–> alias "Macintosh HD:Applications:Calendar.app:"

–バンドルIDで指定
set dPath to path to application id "com.apple.ichat"
–> alias "Macintosh HD:Applications:Messages.app:"

★Click Here to Open This Script 

Posted in File path | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

path toでシステムのさまざまなパスを求める(4文字指定)

Posted on 2月 19, 2018 by Takaaki Naganoya

path toでさまざまなフォルダへのパスを求めるAppleScriptの4文字パラメータで指定するバージョンです。

過去の(Classic MacOS時代の)Scriptとの互換性を維持するために残されている機能のようですが、さすがにファイルクリエータ的なパラメータは今日では好きこのんで使うものでもないので、不思議なpath toの記述を見かけたときに参照する程度でしょうか。

AppleScript名:path toでシステムのさまざまなパスを求める(4文字指定)
set r001 to path to "macs"
–> alias "Macintosh HD:System:"

set r002 to path to "desk"
–> alias "Macintosh HD:Users:me:Desktop:"

–set r003 to path to "sdsk"
–> alias "Macintosh HD:Users:me:Desktop Folder:"

set r004 to path to "trsh"
–> alias "Macintosh HD:Users:me:.Trash:"

set r005 to path to "empt"
–> alias "Macintosh HD:Users:me:.Trash:"

set r006 to path to "font"
–> alias "Macintosh HD:System:Library:Fonts:"

set r007 to path to "pref"
–> alias "Macintosh HD:Users:me:Library:Preferences:"

set r008 to path to "sprf"
–> alias "Macintosh HD:System:Library:PreferencePanes:"

set r009 to path to "temp"
–> alias "Macintosh HD:private:var:folders:h4:jfhlwst88xl9z0001s7k9vk00000gr:T:TemporaryItems:"

set r010 to path to "apps"
–> alias "Macintosh HD:Applications:"

set r011 to path to "docs"
–> alias "Macintosh HD:Users:me:Documents:"

set r012 to path to "root"
–> alias "Macintosh HD:System:"

set r013 to path to "flnt"
–> alias "Macintosh HD:private:var:folders:h4:jfhlwst88xl9z0001s7k9vk00000gr:T:Cleanup At Startup:"

set r014 to path to "asup"
–> alias "Macintosh HD:Library:Application Support:"

set r015 to path to "トhlp"
–> alias "Macintosh HD:Library:Documentation:Help:"

–set r016 to path to "トmod"
–> alias "Macintosh HD:System:Library:Modem Scripts:"

–set r017 to path to "ppdf"
–> alias "Macintosh HD:System:Library:Printers:PPDs:"

set r018 to path to "トscr"
–> alias "Macintosh HD:System:Library:ScriptingAdditions:"

–set r019 to path to "トlib"
–> alias "Macintosh HD:System:Library:CFMSupport:"

set r020 to path to "fvoc"
–> alias "Macintosh HD:System:Library:Speech:Voices:"

set r021 to path to "utiト"
–> alias "Macintosh HD:Applications:Utilities:"

set r022 to path to "aexト"
–> alias "Macintosh HD:Applications:Extras:"

set r023 to path to "cmnu"
–> alias "Macintosh HD:Library:Contextual Menu Items:"

set r024 to path to "prof"
–> alias "Macintosh HD:System:Library:ColorSync:Profiles:"

set r025 to path to "favs"
–> alias "Macintosh HD:Users:me:Library:Favorites:"

set r026 to path to "dtpト"
–> alias "Macintosh HD:Library:Desktop Pictures:"

set r027 to path to "issf"
–> alias "Macintosh HD:Users:me:Library:Internet Search Sites:"

–set r028 to path to "fnds"
–> alias "Macintosh HD:System:Library:Find:"

set r029 to path to "ilgf"
–> alias "Macintosh HD:Library:Receipts:"

set r030 to path to "scrト"
–> alias "Macintosh HD:Users:maro:Library:Scripts:"

set r031 to path to "fasf"
–> alias "Macintosh HD:Users:me:Library:Scripts:Folder Action Scripts:"

set r032 to path to "rapp"
–> alias "Macintosh HD:Users:me:Library:Recent Applications:"

set r033 to path to "rdoc"
–> alias "Macintosh HD:Users:me:Library:Recent Documents:"

set r034 to path to "rsvr"
–> alias "Macintosh HD:Users:me:Library:Recent Servers:"

set r035 to path to "kchn"
–> alias "Macintosh HD:Users:me:Library:Keychains:"

set r036 to path to "qtex"
–> alias "Macintosh HD:System:Library:QuickTime:"

set r037 to path to "usrs"
–> alias "Macintosh HD:Users:"

set r038 to path to "cusr"
–> alias "Macintosh HD:Users:me:"

set r039 to path to "sdat"
–> alias "Macintosh HD:Users:Shared:"

set r040 to path to "rotf"
–> alias "Macintosh HD:System:"

set r041 to path to "csrv"
–> alias "Cherry:System:Library:CoreServices:"

★Click Here to Open This Script 

Posted in File path | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

path toの高度な機能(さまざまなフォルダへのパスを求める)

Posted on 2月 19, 2018 by Takaaki Naganoya

AppleScript名:path toの高度な機能(さまざまなフォルダへのパスを求める)
set aPath to path to system folder
–> alias "Cherry:System:"

set aPath to path to system preferences
–> alias "Cherry:System:Library:PreferencePanes:"

set aPath to path to application support from user domain with folder creation
–> alias "Macintosh HD:Users:maro:Library:Application Support:"

set aPath to path to applications folder
–> alias "Macintosh HD:Applications:"

set aPath to path to desktop from user domain
–> alias "Macintosh HD:Users:maro:Desktop:"

set aPath to path to desktop pictures folder from user domain with folder creation
–> alias "Macintosh HD:Users:maro:Library:Desktop Pictures:"

set aPath to path to documents folder from user domain with folder creation
–> alias "Macintosh HD:Users:maro:Documents:"

set aPath to path to downloads folder from user domain with folder creation
–> alias "Macintosh HD:Users:maro:Downloads:"

set aPath to path to favorites folder from user domain with folder creation
–> alias "Macintosh HD:Users:maro:Library:Favorites:"

set aPath to path to Folder Action scripts folder from user domain
–> alias "Macintosh HD:Users:maro:Library:Scripts:Folder Action Scripts:"

set aPath to path to fonts folder from user domain with folder creation
–> alias "Macintosh HD:Users:maro:Library:Fonts:"

set aPath to path to help folder from user domain with folder creation
–> alias "Macintosh HD:Users:maro:Library:Documentation:Help:"

set aPath to path to home folder from user domain with folder creation
–> alias "Macintosh HD:Users:me:"

set aPath to path to internet plugins folder from user domain with folder creation
–> alias "Macintosh HD:Users:me:Library:Internet Plug-Ins:"

set aPath to path to keychain folder from user domain with folder creation
–> alias "Macintosh HD:Users:me:Library:Keychains:"

set aPath to path to library folder from user domain with folder creation
–> alias "Macintosh HD:Users:me:Library:"

–set aPath to path to modem scripts from user domain with folder creation
–> alias "Macintosh HD:Users:me:Library:Modem Scripts:"

set aPath to path to movies folder from user domain with folder creation
–> alias "Macintosh HD:Users:me:Movies:"

set aPath to path to music folder from user domain with folder creation
–> alias "Macintosh HD:Users:me:Music:"

set aPath to path to pictures folder from user domain with folder creation
–> alias "Macintosh HD:Users:me:Pictures:"

set aPath to path to preferences from user domain with folder creation
–> alias "Macintosh HD:Users:me:Library:Preferences:"

set aPath to path to printer descriptions from user domain with folder creation
–> alias "Macintosh HD:Users:me:Library:Printers:PPDs:"

set aPath to path to public folder from user domain with folder creation
–> alias "Macintosh HD:Users:me:Public:"

set aPath to path to scripting additions folder from user domain with folder creation
–> alias "Macintosh HD:Users:me:Library:ScriptingAdditions:"

set aPath to path to scripts folder from user domain with folder creation
–> alias "Macintosh HD:Users:me:Library:Scripts:"

set aPath to path to services folder from user domain with folder creation
–> alias "Macintosh HD:Users:me:Library:Services:"

set aPath to path to shared documents from user domain with folder creation
–> alias "Cherry:Users:Shared:"

–set aPath to path to shared libraries from user domain with folder creation
set aPath to path to sites folder from user domain with folder creation
–> alias "Macintosh HD:Users:me:Sites:"

set aPath to path to startup disk
–> alias "Cherry:"

set aPath to path to startup items
–> alias "Macintosh HD:Library:StartupItems:"

set aPath to path to temporary items
–> alias "Cherry:private:var:folders:h4:jfhlwst88xl9z0001s7k9vk00000gr:T:TemporaryItems:"

set aPath to path to trash
–> alias "Macintosh HD:Users:me:.Trash:"

set aPath to path to users folder
–> alias "Cherry:Users:"

set aPath to path to workflows folder
–> alias "Macintosh HD:Users:me:Library:Workflows:"

set aPath to path to voices
–> alias "Cherry:System:Library:Speech:Voices:"

–set aPath to path to apple menu
–set aPath to path to ‌control panels
–set aPath to path to control strip modules
–set aPath to path to extensions
–set aPath to path to launcher items folder
–set aPath to path to printer drivers
–set aPath to path to printmonitor
–set aPath to path to shutdown folder
–set aPath to path to speakable items
–set aPath to path to stationery

★Click Here to Open This Script 

Posted in File path | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

選択したファイルのローカライズ名を取得する v2

Posted on 2月 19, 2018 by Takaaki Naganoya
AppleScript名:選択したファイルのローカライズ名を取得する v2
— Created 2014-12-14 by Takaaki Naganoya
— 2014 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

property |NSURL| : a reference to current application’s |NSURL|
property NSURLLocalizedNameKey : a reference to current application’s NSURLLocalizedNameKey

set a to choose folder
–> alias "Macintosh HD:Users:me:Music:"

set b to getLocalizedNameFor(a)
–> {true, "ミュージック"}

on getLocalizedNameFor(aPath)
  set anNSURL to |NSURL|’s fileURLWithPath:(POSIX path of aPath)
  
set {theResult, theValue} to anNSURL’s getResourceValue:(reference) forKey:(NSURLLocalizedNameKey) |error|:(missing value)
  
return {theResult, theValue as string} as list
end getLocalizedNameFor

★Click Here to Open This Script 

Posted in File path | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

Post navigation

  • Older posts
  • Newer posts

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

Google Search

Popular posts

  • 開発機としてM2 Mac miniが来たのでガチレビュー
  • CotEditorで2つの書類の行単位での差分検出
  • macOS 13.6.5 AS系のバグ、一切直らず
  • macOS 15, Sequoia
  • 初心者がつまづきやすい「log」コマンド
  • 指定のWordファイルをPDFに書き出す
  • Pages本執筆中に、2つの書類モード切り替えに気がついた
  • Adobe AcrobatをAppleScriptから操作してPDF圧縮
  • メキシカンハットの描画
  • 与えられた文字列の1D Listのすべての順列組み合わせパターン文字列を返す v3(ベンチマーク用)
  • 2023年に書いた価値あるAppleScript
  • Pixelmator Pro v3.6.4でAppleScriptからの操作時の挙動に違和感が
  • Numbersで選択範囲のセルの前後の空白を削除
  • AdobeがInDesign v19.4からPOSIX pathを採用
  • Safariで「プロファイル」機能を使うとAppleScriptの処理に影響
  • Cocoa Scripting Course 続刊計画
  • AppleScriptによる並列処理
  • macOS 14.xでScript Menuの実行速度が大幅に下がるバグ
  • NaturalLanguage.frameworkでNLEmbeddingの処理が可能な言語をチェック
  • AppleScript入門③AppleScriptを使った「自動化」とは?

Tags

10.11savvy (1102) 10.12savvy (1243) 10.13savvy (1392) 10.14savvy (587) 10.15savvy (438) 11.0savvy (283) 12.0savvy (212) 13.0savvy (187) 14.0savvy (137) 15.0savvy (115) CotEditor (64) Finder (51) iTunes (19) Keynote (115) 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 (75) Pages (54) 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
  • 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年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