AppleScript Script Editor

新しいアプリケーション・スクリプトを作成する


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "Appkit"
use framework "UniformTypeIdentifiers"
use scripting additions
property refMe : a reference to current application


##############################
#####スクリプトメニューから実行させない
##############################
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder"
    set aliasPathToMe to path to me as alias
    tell application "Script Editor"
      open aliasPathToMe
    end tell
return "中止しました"
  end tell
else
  tell current application
    activate
  end tell
end if

set strBundleID to ("com.apple.FontBook") as text
set strAppFileName to (strBundleID & ".app") as text

set strFilePath to ("~/Desktop/" & strAppFileName) as text


set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
set aliasFilePath to (ocidFilePathURL's absoluteURL()) as «class furl»


set strScript to ("#!/usr/bin/env osascript\n----+----1----+----2----+-----3----+----4----+----5----+----6----+----7\n#\n(*\nログイン項目登録用\n起動時に起動後『隠す』処理\nファイル>書き出す…からアプリケーションに書き出し\nログイン項目に登録する\n設定項目のバンドルIDは\nhttps://github.com/force4u/AppleScript/blob/main/Script%20Menu/Developer/Get/getAppBundleID.applescript\nを使って取得できます\n*)\n#com.cocolog-nifty.quicktimer.icefloe\n----+----1----+----2----+-----3----+----4----+----5----+----6----+----7\nuse AppleScript version \"2.8\"\nuse framework \"Foundation\"\nuse scripting additions\n###設定項目\nset strBundleID to (\"" & strBundleID & "\") as text\n###起動\ntell application id strBundleID\nlaunch\nend tell\ndelay 3\n###起動待ち 最大60回\nrepeat 60 times\ntell application id strBundleID\nactivate\nset boolFrontMost to frontmost as boolean\nend tell\nif boolFrontMost is true then\nexit repeat\nelse\ndelay 0.25\nend if\nend repeat\n###名前の取得\ntell application id strBundleID\nset strAppName to name as text\nend tell\n###隠す処理\ntell application \"System Events\"\ntell application process strAppName\nset visible to false\nend tell\nend tell") as text


tell application "Script Editor"
  
  make new document with properties {contents:strScript}
  tell front document
    activate
compile
  end tell
  save front document as "application" in aliasFilePath without run only, startup screen and stay open
  close front document saving no
end tell

|

新規スクリプトを作成する


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#com.cocolog-nifty.quicktimer.icefloe
##
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application

##ファイル名
set strFileName to doGetDateNo("yyyyMMddhhmmss")
set strFileName to (strFileName & ".applescript") as text
set strDateNO to doGetDateNo("yyyyMMdd")

set appFileManager to refMe's NSFileManager's defaultManager()
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDesktopDirURL to ocidURLsArray's firstObject()
set ocidSaveFilePathURL to ocidDesktopDirURL's URLByAppendingPathComponent:(strFileName)

#################################
###ダイアログ用に値を用意
#################################
set strScript to "#!/usr/bin/env osascript\r----+----1----+----2----+-----3----+----4----+----5----+----6----+----7\r#com.cocolog-nifty.quicktimer.icefloe\r#" & strDateNO & "作成\r----+----1----+----2----+-----3----+----4----+----5----+----6----+----7\ruse AppleScript version \"2.8\"\ruse framework \"Foundation\"\ruse framework \"AppKit\"\ruse scripting additions\r\rproperty refMe : a reference to current application\r\r\r"
#################################
###【3】ダイアログを前面に
#################################
tell current application
  set strName to name as text
end tell
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
###ダイアログ
set strIconPath to ("/System/Applications/Utilities/Script Editor.app/Contents/Resources/AppIcon.icns") as text
set aliasIconPath to (POSIX file strIconPath) as alias
set recordResult to (display dialog "スクリプト戻り値です" with title "スクリプト" default answer strScript buttons {"クリップボードにコピー", "キャンセル", "スクリプトエディタで開く"} default button "スクリプトエディタで開く" cancel button "キャンセル" giving up after 20 with icon aliasIconPath without hidden answer)
###クリップボードにコピー
if button returned of recordResult is "クリップボードにコピー" then
  set strText to text returned of recordResult as text
  ####ペーストボード宣言
  set appPasteboard to refMe's NSPasteboard's generalPasteboard()
  set ocidText to (refMe's NSString's stringWithString:(strText))
appPasteboard's clearContents()
appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
end if


###OK押したらスクリプト生成
if button returned of recordResult is "スクリプトエディタで開く" then
  ###スクリプトをテキストで保存
  set ocidScript to refMe's NSString's stringWithString:(strScript)
  set listDone to ocidScript's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF16LittleEndianStringEncoding) |error|:(reference)
  delay 0.5
  set aliasSaveFilePath to (ocidSaveFilePathURL's absoluteURL()) as alias
  ###保存したスクリプトを開く
  tell application "Script Editor"
    open aliasSaveFilePath
  end tell
end if


to doGetDateNo(strDateFormat)
  ####日付情報の取得
  set ocidDate to current application's NSDate's |date|()
  ###日付のフォーマットを定義
  set ocidNSDateFormatter to current application's NSDateFormatter's alloc()'s init()
ocidNSDateFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX")
ocidNSDateFormatter's setDateFormat:strDateFormat
  set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:ocidDate
  set strDateAndTime to ocidDateAndTime as text
return strDateAndTime
end doGetDateNo

|

Script Editor Scripts テンプレート

選択範囲の『後』挿入

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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application

property strBundleID : "com.apple.ScriptEditor2" as text


set strInsertScript to "tell application \"System Events\"\r\ttell process \"SOME APP\"\r\tend tell\rend tell" as text

tell application id strBundleID
  tell the front document
    set strSelectedContents to the contents of selection
  end tell
end tell

set strSelectedContents to strSelectedContents & "\r" & strInsertScript & "\r" & "" as text

tell application id strBundleID
  tell the front document
    set contents of selection to strSelectedContents
    set selection to {}
  end tell
end tell

|

[applescript://]スキームapplescriptのデフォルトのエディタを変更する


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
(*
com.cocolog-nifty.quicktimer.icefloe
applescript://
*)
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application


###設定項目ドキュメントのURL
set strScheme to "applescript://" as text
###NSURL
set ocidScheme to refMe's NSURL's URLWithString:(strScheme)
###ワークスペース初期化
set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace()
###URLタイプのデフォルトアプリケーション
set ocidAppPathURL to appShardWorkspace's URLsForApplicationsToOpenURL:(ocidScheme)

###ダイアログ用のアプリケーション名リスト
set listAppName to {} as list
###アプリケーションのURLを参照させるためのレコード
set ocidBrowserDictionary to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
####################################################
####httpがスキームとして利用可能なアプリケーション一覧を取得する
####################################################
repeat with itemAppPathURL in ocidAppPathURL
  ###アプリケーションの名前
  set listResponse to (itemAppPathURL's getResourceValue:(reference) forKey:(refMe's NSURLNameKey) |error|:(missing value))
  set strAppName to (item 2 of listResponse) as text
  log "ブラウザの名前は:" & strAppName & "です"
  copy strAppName to end of listAppName
  ####パス
  set aliasAppPath to itemAppPathURL's absoluteURL() as alias
  log "ブラウザのパスは:" & aliasAppPath & "です"
  ####バンドルID取得
  set ocidAppBunndle to (refMe's NSBundle's bundleWithURL:(itemAppPathURL))
  set ocidBunndleID to ocidAppBunndle's bundleIdentifier
  set strBundleID to ocidBunndleID as text
  log "ブラウザのBunndleIDは:" & strBundleID & "です"
(ocidBrowserDictionary's setObject:(itemAppPathURL) forKey:(strAppName))
end repeat

################################
##ダイアログ
################################
###ダイアログを前面に
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder"
    activate
  end tell
else
  tell current application
    activate
  end tell
end if
try
  set listResponse to (choose from list listAppName with title "選んでください" with prompt "URLを開くアプリケーションを選んでください" default items (item 1 of listAppName) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed)
on error
  log "エラーしました"
return "エラーしました"
end try
if listResponse is false then
return "キャンセルしました"
end if
set strResponse to (item 1 of listResponse) as text
################################
##アプリケーションのURLを取得する
################################
###アプリケーションのURLを取り出す
set ocidAppPathURL to ocidBrowserDictionary's objectForKey:(strResponse)
log className of ocidAppPathURL as text
log ocidAppPathURL as alias

################################
##デフォルトに設定する
################################

appShardWorkspace's setDefaultApplicationAtURL:(ocidAppPathURL) toOpenURLsWithScheme:("applescript") completionHandler:(missing value)





|

"applescript://com.apple.scripteditor" オプション

ファイル名指定


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



action=new
name=ファイル名
script=%エンコードされたスクリプト

|

スクリプトメニューやコマンドラインから実行させない

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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions


##############################
#####スクリプトメニューから実行させない
##############################
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder"
    set aliasPathToMe to path to me as alias
    tell application "Script Editor"
      open aliasPathToMe
    end tell
    return "中止しました"
  end tell
else
  tell current application
    activate
  end tell
end if



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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
(*
com.cocolog-nifty.quicktimer.icefloe
*)
#
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

##############################
#####ダイアログを前面に
##############################
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder" to activate
doDisplayAlert()
else
  tell current application to activate
doDisplayAlert()
end if



on doDisplayAlert()
  
  display alert ""
  
end doDisplayAlert

|

[SCPT]拡張子scptのスクリプトを拡張子applescriptのテキストに変換する

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


tell application "Finder"
  set listAliasPath to selection as list
end tell


doScriptRun(listAliasPath)

on doScriptRun(argFileInput)
  set numCntSelectedObject to (count of argFileInput) as integer
  if numCntSelectedObject = 0 then
    return "未選択です"
  end if
  repeat with itemFilePath in argFileInput
    set aliasFilePath to itemFilePath as alias
    set objInfo to get info for aliasFilePath
    if (folder of objInfo) is true then
      log "フォルダなので処理しない"
    else if (type identifier of objInfo) is "com.apple.applescript.text" then
      log "すでにテキスト形式なので処理しない"
    else if (type identifier of objInfo) is "com.apple.applescript.script" then
      set strExtension to name extension of objInfo
      set strNewExtension to "applescript"
      set strName to (name of objInfo) as text
      tell application "Finder"
        set aliasSaveDirPath to (container of aliasFilePath) as alias
        set strSaveDirPath to POSIX path of aliasSaveDirPath as text
      end tell
      set strFilePath to POSIX path of aliasFilePath as text
      set strSaveFilePath to doReplace(strFilePath, ".scpt", ".applescript") as text
      set aliasSavePath to POSIX file strSaveFilePath as «class furl»
      
      tell application "Script Editor"
        open aliasFilePath
        save the front document as "text" in aliasSavePath
        close front document
      end tell
      set theComandText to ("chmod 755  \"" & strSaveFilePath & "\"") as text
      do shell script theComandText
      tell application "Finder"
        move aliasFilePath to trash
      end tell
    else
      log "スクリプト形式以外は処理しない"
    end if
  end repeat
end doScriptRun


to doReplace(theText, orgStr, newStr)
  set oldDelim to AppleScript's text item delimiters
  set AppleScript's text item delimiters to orgStr
  set tmpList to every text item of theText
  set AppleScript's text item delimiters to newStr
  set tmpStr to tmpList as text
  set AppleScript's text item delimiters to oldDelim
  return tmpStr
end doReplace

|

[Open]スクリプトを開くスクリプト

ダウンロード - openscript.zip

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
#                       com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application

set objFileManager to refMe's NSFileManager's defaultManager()

###########################
#####このファイルへのパス
set aliasPathToMe to path to me as alias
#####
set strPathToMe to POSIX path of aliasPathToMe as text
set ocidPathToMeStr to refMe's NSString's stringWithString:strPathToMe
set ocidPathToMePath to ocidPathToMeStr's stringByStandardizingPath()
set ocidPathToMePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:ocidPathToMePath isDirectory:false

set ocidContainerDirURL to ocidPathToMePathURL's URLByDeletingLastPathComponent()
set ocidContentsDirPathURL to ocidContainerDirURL's URLByAppendingPathComponent:"Scripts" isDirectory:true
###########################
set ocidPropertiesForKeys to {refMe's NSURLPathKey, refMe's NSURLNameKey}
set ocidOption to refMe's NSDirectoryEnumerationSkipsHiddenFiles
set listResult to objFileManager's contentsOfDirectoryAtURL:ocidContentsDirPathURL includingPropertiesForKeys:ocidPropertiesForKeys options:ocidOption |error|:(reference)
set ocidContentsArray to item 1 of listResult
log ocidContentsArray as list

###########################
##TURE値
set ocidTrue to (refMe's NSNumber's numberWithBool:true)
##格納用のARRAY
set ocidFileNameArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
###URLの数だけ繰り返し
repeat with itemContentsArray in ocidContentsArray
    ###レギュラーファイルキーを取得
    set listResult to (itemContentsArray's getResourceValue:(reference) forKey:(refMe's NSURLIsRegularFileKey) |error|:(reference))
    ###ファイル?
    set boolIsRegularFileKey to item 2 of listResult
    ###ファイルならリストに加える
    if boolIsRegularFileKey is ocidTrue then
        set ocidFileName to itemContentsArray's lastPathComponent()
        (ocidFileNameArray's addObject:ocidFileName)
    end if
end repeat
####並び替え
set ocidSortArray to ocidFileNameArray's sortedArrayUsingSelector:"localizedCaseInsensitiveCompare:"
set listSortArray to ocidSortArray as list

###########################
#####ファイル名のリストでダイアログ
try
    set objResponse to (choose from list listSortArray with title "選んでください" with prompt "開くスクリプトを選んでください" default items (item 1 of listSortArray) OK button name "OK" cancel button name "キャンセル" with multiple selections allowed without empty selection allowed)
on error
    log "エラーしました"
    return
end try
if objResponse is false then
    log "キャンセルしました"
    return
end if
######選んだ戻り値
set strFileName to (objResponse) as text

###########################
#####開くファイルのURL
set ocidOpenFilePathURL to ocidContentsDirPathURL's URLByAppendingPathComponent:strFileName isDirectory:false
log className() of ocidOpenFilePathURL as text
log ocidOpenFilePathURL's |Path|
set aliasFilePathURL to ocidOpenFilePathURL as alias
####開く
tell application "Script Editor"
    launch
    activate
    open aliasFilePathURL
end tell

|

[Script Editor]スクリプトの内容をCotEditorのカラーリングでHTMLにする

#!/usr/bin/env osascript

----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

#

#

#

#

# com.cocolog-nifty.quicktimer.icefloe

----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

##自分環境がos12なので2.8にしているだけです

use AppleScript version "2.8"

use framework "Foundation"

use scripting additions



tell application "Script Editor"

    log name as text

    tell front document

        properties

        set objContents to contents

    end tell

    set strContents to objContents as text

end tell


##############################

#### 中間ファイル

##############################

set strSaveDirPath to "/private/tmp/" as text

set aliasSaveDirPath to POSIX file strSaveDirPath as alias

set strTime to (do shell script "date +%s") as text

####

set strHtmlFileName to ("" & strTime & ".html") as text

set strTextFileName to ("" & strTime & ".txt") as text

set strRtfFileName to ("" & strTime & ".rtf") as text

set strAppleScriptFileName to ("" & strTime & ".applescript") as text

######

set strHtmlFilePath to ("" & strSaveDirPath & strHtmlFileName & "") as text

set strTextFilePath to ("" & strSaveDirPath & strTextFileName & "") as text

set strRtfFilePath to ("" & strSaveDirPath & strRtfFileName & "") as text

set strAppleScriptFilePath to ("" & strSaveDirPath & strAppleScriptFileName & "") as text





##############################

####applescriptテキストファイルを作成する

##############################

tell application "Finder"

    try

        make new file at aliasSaveDirPath with properties {name:(strAppleScriptFileName)}

    end try

end tell

set aliasAppleScriptFilePath to POSIX file strAppleScriptFilePath as alias

##############################

#### CotEditorにスクリプトを移す

##############################

tell application "CotEditor"

    set objActivDoc to open aliasAppleScriptFilePath

    tell objActivDoc

        set contents of selection to strContents

    end tell

end tell

##############################

#### 選択範囲のテキストを取得

##############################

tell application "CotEditor"

    activate

    tell application "System Events"

        tell process "CotEditor"

            tell menu bar 1

                click menu item "すべてを選択" of menu "編集"

                try

                    click menu item "リッチテキストとしてコピー" of menu "編集"

                end try

            end tell

        end tell

    end tell

end tell

##############################

#### CotEditorにスクリプトを移す

##############################

tell application "CotEditor"

    tell front document

        close saving no

    end tell

end tell

##############################

####まずはテキストファイルを作成する

##############################

tell application "Finder"

    try

        make new file at aliasSaveDirPath with properties {name:(strTextFileName)}

    end try

end tell

set aliasAppleScriptFilePath to POSIX file strTextFilePath as alias

##############################

####RTFファイルに変換する

##############################

do shell script "/usr/bin/textutil -convert rtf \"" & strTextFilePath & "\"  -output \"" & strRtfFilePath & "\""

##############################

####RTFファイルを開く

##############################

set aliasRtfFilePath to POSIX file strRtfFilePath as alias

######

tell application "TextEdit"

    activate

    open aliasRtfFilePath

end tell

##############################

#### ペーストして

##############################

tell application "TextEdit"

    activate

    tell application "System Events"

        tell process "TextEdit"

            tell menu bar 1

                click menu item "ペースト" of menu "編集"

                try

                    click menu item "編集" of menu "すべてを選択"

                end try

            end tell

        end tell

    end tell

end tell

##############################

#### 上書き保存して

##############################

tell application "TextEdit"

    activate

    tell document 1

        close saving yes

    end tell

end tell

##############################

#### HTMLで別名保存する

##############################

tell application "TextEdit"

    activate

    open aliasRtfFilePath

    tell document 1

        activate

        save in (POSIX file strHtmlFilePath)

        close

    end tell

end tell

set aliasHtemlFilePath to POSIX file strHtmlFilePath as alias


##############################

#### HTMLをCotEditorで開いて加工する

##############################

tell application "CotEditor"

    open aliasHtemlFilePath

    tell front document

        ####置換はお好みで

        replace for "\n|\r" to "" with all, ignore case and RE without backwards and wrap

        replace for "\t" to "    " with all, ignore case and RE without backwards and wrap

        replace for "^(.*<body>)|</body>|</html>" to "" with all, ignore case and RE without backwards and wrap

        replace for "font-size: 16px !important;font-family: ui-monospace,monospace !important;" to "font-size: 16px !important;font-family: ui-monospace,monospace !important;" with all, ignore case and RE without backwards and wrap

        replace for "padding-left: 8px !important;background-color: #2A2A29; margin: 0px 0px 0px 0px !important;" to "padding-left: 8px !important;background-color: #2A2A29; margin: 0px 0px 0px 0px !important;" with all, ignore case and RE without backwards and wrap

        replace for "^<p" to "<div class=\"sourceCode\"><p" with all, ignore case and RE without backwards and wrap

        replace for ">$" to "></div>" with all, ignore case and RE without backwards and wrap

        save

    end tell

end tell

|

[SCPT]拡張子scptのスクリプトを拡張子applescriptのテキストに変換する

自分用
ついついscptで作っちゃうんだよね



#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application
property refNSString : a reference to refMe's NSString
property refNSURL : a reference to refMe's NSURL

set objFileManager to refMe's NSFileManager's defaultManager()

###################################
#####入力フォルダ
###################################
###ダイアログのデフォルト
set ocidUserDesktopPath to (objFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set aliasDefaultLocation to ocidUserDesktopPath as alias

###ダイアログテキスト
set strPromptText to "入力フォルダを選んでください"
try
set listResponse to (choose folder strPromptText with prompt strPromptText default location aliasDefaultLocation without multiple selections allowed, invisibles and showing package contents) as list
on error
log "エラーしました"
return
end try
###エリアス
set aliasDirPath to (item 1 of listResponse) as alias
###UNIXパス
set strDirPath to POSIX path of aliasDirPath as text
###String
set ocidDirPath to refNSString's stringWithString:strDirPath
###NSURL
set ocidDirPathURL to refNSURL's alloc()'s initFileURLWithPath:ocidDirPath isDirectory:true
####Path
set ocidDirPath to ocidDirPathURL's |path|()
##set ocidSubPathArray to objFileManager's subpathsAtPath:ocidDirPath
set ocidSubPathArray to objFileManager's subpathsOfDirectoryAtPath:ocidDirPath |error|:(reference)

repeat with itemSubPath in (item 1 of ocidSubPathArray)
####フルパスにする
set ocdiFilePath to (ocidDirPath's stringByAppendingPathComponent:itemSubPath)
set strExtensionName to ocdiFilePath's pathExtension() as text
set strSubPath to itemSubPath as text

if strSubPath contains ".app/" then
log "アプリケーションの中にある場合は処理しない"

else if strSubPath contains ".scptd/" then
log "スクリプトライブラリの中にある場合は処理しない"

else if strSubPath contains ". xcodeproj/" then
log "xcodeprojの中にある場合は処理しない"


else if strSubPath contains ".framework/" then
log "frameworkの中にある場合は処理しない"

else if strExtensionName is "scpt" then
####元ファイル
set ocidScptPathURL to (refNSURL's alloc()'s initFileURLWithPath:ocdiFilePath isDirectory:false)
set aliasScptPath to ocidScptPathURL as alias
log aliasScptPath
####変換先ファイル
set ocidAppleScptPath to ocdiFilePath's stringByDeletingPathExtension()
set ocidAppleScptPath to (ocidAppleScptPath's stringByAppendingPathExtension:"applescript")
####同名のファイルがすでに存在するか?チェック
set boolFileExists to (objFileManager's fileExistsAtPath:ocidAppleScptPath isDirectory:true)
if boolFileExists is true then
####同名のファイルがある場合は修正日をファイル名に付与する
set listResult to (ocidScptPathURL's getResourceValue:(reference) forKey:(refMe's NSURLAttributeModificationDateKey) |error|:(reference))
set ocidModeDate to (item 2 of listResult)
####日付のフォーマットの初期化
set ocidNSDateFormatter to refMe's NSDateFormatter's alloc()'s init()
####日本語指定しておく
(ocidNSDateFormatter's setLocale:(refMe's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX"))
####フォーマット指定
(ocidNSDateFormatter's setDateFormat:"yyyyMMDD.hhmm")
####フォーマットで日付を変換
set ocidDateAndTime to (ocidNSDateFormatter's stringFromDate:ocidModeDate)
####日付なのでゼロサプレス対策でテキストで確定させておく
set strDateAndTime to ocidDateAndTime as text
####まずファイル名を取得
set ocidScptFileName to ocdiFilePath's lastPathComponent()
####拡張子を取ってベースファイル名
set strScptFileName to ocidScptFileName's stringByDeletingPathExtension() as text
####ファイル名に日付を入れた新しいファイル名
set strNewFileName to (strScptFileName & "." & strDateAndTime) as text
####ファイル名を除いたファイルの所在ディレクトリ
set ocidContainterPath to ocdiFilePath's stringByDeletingLastPathComponent()
####↑に新しいファイル名を付与して新しいパスとする
set ocidNewFilepath to (ocidContainterPath's stringByAppendingPathComponent:strNewFileName)
####拡張子を付与して新しいファイルパス
set ocidAppleScptPath to (ocidNewFilepath's stringByAppendingPathExtension:"applescript")
end if
####NSURL
set ocidAppleScptURL to (refNSURL's alloc()'s initFileURLWithPath:ocidAppleScptPath isDirectory:false)

#####変換後のFinderエイリアスはこの時点で存在しないのでfurl
set aliasAppleScptURL to ocidAppleScptURL as «class furl»
tell application "Script Editor"
try
with timeout of 10 seconds
set objActivDoc to open aliasScptPath
end timeout
if objActivDoc is (missing value) then
log "オープンに失敗"
close last window
else
save objActivDoc as "text" in aliasAppleScptURL
close front window
end if
on error
close front window
end try
end tell

set boolFileExists to (objFileManager's fileExistsAtPath:ocidAppleScptPath isDirectory:true)
if boolFileExists is false then
log "ファイルの変換に失敗したのでそのままにする"
else if boolFileExists is true then
log "変換に成功したので元ファイルをゴミ箱に入れます"
set listResult to (objFileManager's trashItemAtURL:ocidScptPathURL resultingItemURL:(missing value) |error|:(reference))
end if

end if

end repeat

|

その他のカテゴリー

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