AppleScript BackUp

フォルダーコピー(Mac固有の情報をコピーしない)


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

#!/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 application "Finder"
  set aliasDefaultLocation to (path to desktop folder from user domain) as alias
end tell
######元
set strMes to "コピーする元フォルダを選んでください" as text
set strPrompt to "フォルダを選択してください" as text
try
  set aliasOrigDirPath to (choose folder strMes with prompt strPrompt default location aliasDefaultLocation with invisibles and showing package contents without multiple selections allowed) as alias
on error
  log "エラーしました"
return
end try
######先
set strMes to "コピー先のフォルダを選んでください" as text
set strPrompt to "フコピー先のォルダを選択してください" as text
try
  set aliasDistDirPath to (choose folder strMes with prompt strPrompt default location aliasDefaultLocation with invisibles and showing package contents without multiple selections allowed) as alias
on error
  log "エラーしました"
return
end try

###パス整形
set strOrigDirPath to POSIX path of aliasOrigDirPath as text
set strDistDirPath to POSIX path of aliasDistDirPath as text
###機種依存置き換え(エラー対策)
set strOrigDirPath to doReplace(strOrigDirPath, "`", "")
set strDistDirPath to doReplace(strDistDirPath, "`", "")
###ディスクフォーマットの違いによりコマンドの分岐
set aliasDistDirPath to POSIX file strDistDirPath as alias
###コピー先のディスク名
tell application "Finder"
  ###エイリアスを分解
  set fileFolderPath to folder aliasDistDirPath
  ###ディスク部分を取得
  set refDiskName to disk of fileFolderPath
  ###起動ディスク配下
  if refDiskName is startup disk then
    tell startup disk
      set strVolumeName to name as text
    end tell
  else
    set strVolumeName to refDiskName as text
  end if
end tell
####コピー先のディスクのフォーマット
tell application "System Events"
  tell disk strVolumeName
    set strDiskFormat to format as text
    ###通常コピー
    if strDiskFormat contains "Mac" then
      set strCommandText to ("/usr/bin/ditto \"" & strOrigDirPath & "\" \"" & strDistDirPath & "\"") as text
    else if strDiskFormat contains "NFS" then
      set strCommandText to ("/usr/bin/ditto \"" & strOrigDirPath & "\" \"" & strDistDirPath & "\"") as text
    else if strDiskFormat contains "APFS" then
      set strCommandText to ("/usr/bin/ditto \"" & strOrigDirPath & "\" \"" & strDistDirPath & "\"") as text
    else if strDiskFormat contains "Apple" then
      set strCommandText to ("/usr/bin/ditto \"" & strOrigDirPath & "\" \"" & strDistDirPath & "\"") as text
    else
      ###Mac固有の情報をコピーしない
      set strCommandText to ("/usr/bin/ditto --norsrc \"" & strOrigDirPath & "\" \"" & strDistDirPath & "\"") as text
    end if
  end tell
end tell


do shell script strCommandText




to doReplace(argOrignalText, argSearchText, argReplaceText)
  set strDelim to AppleScript's text item delimiters
  set AppleScript's text item delimiters to argSearchText
  set listDelim to every text item of argOrignalText
  set AppleScript's text item delimiters to argReplaceText
  set strReturn to listDelim as text
  set AppleScript's text item delimiters to strDelim
return strReturn
end doReplace

|

[バックアップ]差分バックアップ(同名ファイルは処理しない)


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

#!/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 appFileManager to refMe's NSFileManager's defaultManager()

property refNSNotFound : a reference to 9.22337203685477E+18 + 5807

###コピー元 ディレクトリ
set strOrigDirPath to "/Volumes/HD-EDS-B/Shared/Musics/Music" as text
###保存先 ディレクトリ
set strDistDirPath to "/Volumes/NO NAME/contents/Musics/Music" as text

##対象のファイルのUTI サンプルは音楽ファイル
set listUTI to {"com.apple.m4a-audio", "public.mp3"} as list



#################################
###コピー元
set ocidOrigDirPathStr to refMe's NSString's stringWithString:(strOrigDirPath)
set ocidOrigDirPath to ocidOrigDirPathStr's stringByStandardizingPath()
set ocidOrigDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidOrigDirPath) isDirectory:true)

###コピー元
set ocidDistDirPathStr to refMe's NSString's stringWithString:(strDistDirPath)
set ocidDistDirPath to ocidDistDirPathStr's stringByStandardizingPath()
set ocidDistDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidDistDirPath) isDirectory:true)

#################################
###コンテンツ収集最下層まで
##プロパティ
set ocidProperty to {(refMe's NSURLIsDirectoryKey), (refMe's NSURLPathKey), (refMe's NSURLIsRegularFileKey), (refMe's NSURLContentTypeKey)}
set ocidOptions to (refMe's NSDirectoryEnumerationSkipsHiddenFiles)
set ocidEmuDict to appFileManager's enumeratorAtURL:(ocidOrigDirPathURL) includingPropertiesForKeys:(ocidProperty) options:(ocidOptions) errorHandler:(reference)
##Arrayに格納
set ocidEmuFileArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
set ocidEmuObject to (ocidEmuDict's allObjects)
ocidEmuFileArray's addObjectsFromArray:(ocidEmuObject)

#################################
###URL格納用のArray
set ocidFileURLArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
###全URLから対象のファイルパスを取得します
repeat with itemEmuFileURL in ocidEmuFileArray
  ####URLをforKeyで取り出し
  set listResult to (itemEmuFileURL's getResourceValue:(reference) forKey:(refMe's NSURLIsRegularFileKey) |error|:(reference))
  ###リストからNSURLIsRegularFileKeyのBOOLを取り出し
  set boolIsRegularFileKey to item 2 of listResult
  ####ファイルのみを(ディレクトリやリンボリックリンクは含まない)
  if boolIsRegularFileKey is (refMe's NSNumber's numberWithBool:true) then
    set listResult to (itemEmuFileURL's getResourceValue:(reference) forKey:(refMe's NSURLContentTypeKey) |error|:(reference))
    set ocidContentType to item 2 of listResult
    set strUTI to (ocidContentType's identifier) as text
    ####対象のファイルだけリストに加える
    if listUTI contains strUTI then
      ####リストにする
(ocidFileURLArray's addObject:(itemEmuFileURL))
    end if
  end if
end repeat

#################################
###本処理
repeat with itemFileURL in ocidFileURLArray
  ###対象のパス
  set strItemFilePath to (itemFileURL's |path|) as text
  set ocidItemPathString to (refMe's NSMutableString's alloc()'s initWithCapacity:0)
(ocidItemPathString's setString:(strItemFilePath))
  ###レンジを取得
  set ocidPathRange to refMe's NSMakeRange(0, ocidItemPathString's |length|)
  ###元のファイルパスから保存先ファイルパスを生成
  ##ディレクトリパスの置換
  set ocidOption to refMe's NSCaseInsensitiveSearch
(ocidItemPathString's replaceOccurrencesOfString:(ocidOrigDirPathStr) withString:(ocidDistDirPathStr) options:(ocidOption) range:(ocidPathRange))
  ###コマンド用のテキストにして
  set strDistFilePath to ocidItemPathString as text
  ###コピー先パス
  set ocidDistFilePath to ocidItemPathString's stringByStandardizingPath()
  ###ファイルの有無を確認
  set boolFileExist to (appFileManager's fileExistsAtPath:(ocidDistFilePath) isDirectory:(false))
  ###ファイルがなければ
  if boolFileExist = false then
    ###コピーする
    set strCommandText to ("/usr/bin/ditto \"" & strItemFilePath & "\" \"" & strDistFilePath & "\"") as text
    do shell script strCommandText
  end if
  
end repeat

display notification "処理終了" with title "処理が終了" subtitle "処理が終了しました" sound name "Sonumi"
log ">>>>>>>>>>>>処理終了<<<<<<<<<<<<<<<"
return ">>>>>>>>>>>>処理終了<<<<<<<<<<<<<<<"

|

[iPhone]バックアップの容量を調べる

#!/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 application "Finder"
    set aliasApplicationSupportFolder to path to application support folder from user domain as alias
end tell


### iPhoneのバックアップフォルダ
tell application "Finder"
    set aliasBackupFolder to folder "Backup" of folder "MobileSync" of folder aliasApplicationSupportFolder as alias
end tell

###  バックアップフォルダの中身
###ここで『フォルダの中身』を取得しています
tell application "Finder"
    set listInSideFolder to (list folder aliasBackupFolder without invisibles) as list
end tell

if listInSideFolder is {} then
    return "【情報】iPhoneのバックアップは,このMacにはありません"
end if


#### 順番にフォルダを処理する
repeat with objInSideFolder in listInSideFolder
    set strInSideFolder to objInSideFolder as text
    tell application "Finder"
        set aliasObjInSideFolder to folder strInSideFolder of folder aliasBackupFolder as alias
        tell folder aliasObjInSideFolder
            --->通常の処理はここで
            set strModificationDate to modification date as text
            log "バックアップは," & strModificationDate & "に取得した内容です"
        end tell
    end tell
    ####フォルダのサイズを取得します
    tell application "System Events" to set numFolderSize to size of aliasObjInSideFolder as number
    set strFolderSize to numFolderSize as text
    set AppleScript's text item delimiters to "E"
    set listFolderSize to every text item of strFolderSize
    set AppleScript's text item delimiters to ""
    set strFolderSize to item 1 of listFolderSize as text
    if strFolderSize contains "." then
        log "バックアップの容量は、" & strFolderSize & " GBです"
        return "バックアップの容量は、" & strFolderSize & " GBです"
    else if numFolderSize > 99999 then
        set strFolderSize to (numFolderSize / 100000) as text
        log "バックアップの容量は、" & strFolderSize & " MBです"
        return "バックアップの容量は、" & strFolderSize & " MBです"
    else
        set strFolderSize to (numFolderSize / 1000) as text
        log "バックアップの容量は、" & strFolderSize & " KBです"
        return "バックアップの容量は、" & strFolderSize & " KBです"
    end if
end repeat


set numCntBackUpFolder to (count of listInSideFolder) as number
if numCntBackUpFolder > 1 then
    log "【警告】このMacには," & numCntBackUpFolder & "台分のiPhoneのバックアップがあります"
    return "【警告】このMacには," & numCntBackUpFolder & "台分のiPhoneのバックアップがあります"
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

property refMe : a reference to current application

set objFileManager to refMe's NSFileManager's defaultManager()
##############################
### iPhoneのバックアップフォルダ
##############################
set ocidUserApplicationSupport to (objFileManager's URLsForDirectory:(refMe's NSApplicationSupportDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidUserApplicationSupportURL to item 1 of ocidUserApplicationSupport
set ocidBackupFilePathURL to ocidUserApplicationSupportURL's URLByAppendingPathComponent:"MobileSync/Backup" isDirectory:true
#####フォルダ等の有無を確認
set listSubPathResult to (objFileManager's contentsOfDirectoryAtURL:ocidBackupFilePathURL includingPropertiesForKeys:{refMe's NSURLPathKey} options:(refMe's NSDirectoryEnumerationSkipsHiddenFiles) |error|:(reference))
set ocidBackupFileArray to item 1 of listSubPathResult

log "バックアップは" & (count of ocidBackupFileArray) & "点あります"
##############################
### サイズチェック
##############################

repeat with itemBackupFileArray in ocidBackupFileArray
    ###フォルダ名
    set ocidFileName to itemBackupFileArray's lastPathComponent()
    ####フォルダの下層までenumeratorで取得する
    set numDirSize to 0
    set ocidEmuDict to (objFileManager's enumeratorAtURL:itemBackupFileArray includingPropertiesForKeys:{refMe's NSURLTotalFileSizeKey} options:0 errorHandler:(reference))
    #### enumeratorからURLのリストにする
    set ocidEmuFileURLArray to ocidEmuDict's allObjects()
    ####URLの分だけ繰り返し
    repeat with itemEmuFileURL in ocidEmuFileURLArray
        ###ファイルサイズを取得して
        set listResult to (itemEmuFileURL's getResourceValue:(reference) forKey:(refMe's NSURLTotalFileSizeKey) |error|:(reference))
        set ocidURLSIze to item 2 of listResult
        ####値が戻れば加算していく
        if ocidURLSIze is not (missing value) then
            set numDirSize to numDirSize + (ocidURLSIze as real)
        end if
    end repeat
    ####合計サイズを整形
    set numDirSize to numDirSize / (1024 ^ 3) as integer
    
    log "フォルダ" & (ocidFileName as text) & "のサイズは,約" & numDirSize & "GBです"
    
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