
【macOS】Microsoft Office For macOS 新規書類作成時のデフォルト設定
スクリプト単体では動作しません試したい場合は
こちらからダウンロードしてください
| ソース | |
|---|---|
| 001 | #!/usr/bin/env osascript |
| 002 | #coding: utf-8 |
| 003 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7-- |
| 004 | (* |
| 005 | Microsoft Officeのスタートアップテンプレートを |
| 006 | バックアップして |
| 007 | その後で |
| 008 | 設定済みの書類をコピーします |
| 009 | すでに設定済みの場合はバックアップのみで |
| 010 | 置き換えしないようにしてありますので |
| 011 | 安心して使えます |
| 012 | バックアップ先 |
| 013 | /Users/ユーザーID/Documents/Microsoft Office/バックアップ |
| 014 | |
| 015 | v1 初回作成 バックアップ機能付き |
| 016 | v1.1 すでに設定済みの場合は上書き『しない』ようにした |
| 017 | |
| 018 | 1:Excel |
| 019 | /Users/ユーザーID/Library/Group Containers/UBF8T346G9.Office/User Content.localized/Startup.localized/Excel/book.xltx |
| 020 | |
| 021 | 2:Word |
| 022 | /Users/ユーザーID/Library/Group Containers/UBF8T346G9.Office/User Content.localized/Templates.localized/Normal.dotm |
| 023 | |
| 024 | 3:PowerPoint |
| 025 | /Users/ユーザーID/Library/Group Containers/UBF8T346G9.Office/User Content.localized/Themes.localized/Default Theme.thmx |
| 026 | |
| 027 | |
| 028 | |
| 029 | |
| 030 | |
| 031 | com.cocolog-nifty.quicktimer.icefloe *) |
| 032 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7-- |
| 033 | use AppleScript version "2.8" |
| 034 | use framework "Foundation" |
| 035 | use framework "AppKit" |
| 036 | use framework "UniformTypeIdentifiers" |
| 037 | use scripting additions |
| 038 | property refMe : a reference to current application |
| 039 | ########################### |
| 040 | #バックアップフォルダ |
| 041 | set strDateNo to doGetDateNo("yyyyMMdd_HHmmss") as text |
| 042 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 043 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDocumentDirectory) inDomains:(refMe's NSUserDomainMask)) |
| 044 | set ocidDocumentDirPathURL to ocidURLsArray's firstObject() |
| 045 | set strSubPath to ("Microsoft Office/バックアップ/ユーザー コンテンツ" & strDateNo & "") as text |
| 046 | set ocidBackupDirPathURL to ocidDocumentDirPathURL's URLByAppendingPathComponent:(strSubPath) isDirectory:(true) |
| 047 | # |
| 048 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 049 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init() |
| 050 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
| 051 | set listDone to appFileManager's createDirectoryAtURL:(ocidBackupDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error|:(reference) |
| 052 | ########################### |
| 053 | #設定の対象フォルダ |
| 054 | set strContentDirPath to ("~/Library/Group Containers/UBF8T346G9.Office/User Content.localized") as text |
| 055 | set strTemplatesDirPath to ("~/Library/Group Containers/UBF8T346G9.Office/User Content.localized/Templates.localized") as text |
| 056 | set strStartupDirPath to ("~/Library/Group Containers/UBF8T346G9.Office/User Content.localized/Startup.localized") as text |
| 057 | set strThemesDirPath to ("~/Library/Group Containers/UBF8T346G9.Office/User Content.localized/Themes.localized") as text |
| 058 | #User Content |
| 059 | set ocidContentDirPathStr to refMe's NSString's stringWithString:(strContentDirPath) |
| 060 | set ocidContentDirPath to ocidContentDirPathStr's stringByStandardizingPath() |
| 061 | set ocidContentDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidContentDirPath) isDirectory:true) |
| 062 | #Templates |
| 063 | set ocidTemplatesDirPathStr to refMe's NSString's stringWithString:(strTemplatesDirPath) |
| 064 | set ocidTemplatesDirPath to ocidTemplatesDirPathStr's stringByStandardizingPath() |
| 065 | set ocidTemplatesDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidTemplatesDirPath) isDirectory:true) |
| 066 | #Startup |
| 067 | set ocidStartupDirPathStr to refMe's NSString's stringWithString:(strStartupDirPath) |
| 068 | set ocidStartupDirPath to ocidStartupDirPathStr's stringByStandardizingPath() |
| 069 | set ocidStartupDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidStartupDirPath) isDirectory:true) |
| 070 | #Themes |
| 071 | set ocidThemesDirPathStr to refMe's NSString's stringWithString:(strThemesDirPath) |
| 072 | set ocidThemesDirPath to ocidThemesDirPathStr's stringByStandardizingPath() |
| 073 | set ocidThemesDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidThemesDirPath) isDirectory:true) |
| 074 | ########################### |
| 075 | set boolWarning to false as boolean |
| 076 | #フォルダ名を修正 |
| 077 | set boolDirExists to appFileManager's fileExistsAtPath:(ocidTemplatesDirPath) isDirectory:(true) |
| 078 | if boolDirExists is false then |
| 079 | log "【警告】:テンプレートフォルダが見つかりません" |
| 080 | set listDone to appFileManager's createDirectoryAtURL:(ocidTemplatesDirPath) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error|:(reference) |
| 081 | set boolWarning to true as boolean |
| 082 | end if |
| 083 | #フォルダ名を修正 |
| 084 | set boolDirExists to appFileManager's fileExistsAtPath:(ocidStartupDirPath) isDirectory:(true) |
| 085 | if boolDirExists is false then |
| 086 | log "【警告】:スタートアップフォルダが見つかりません" |
| 087 | set listDone to appFileManager's createDirectoryAtURL:(ocidStartupDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error|:(reference) |
| 088 | set boolWarning to true as boolean |
| 089 | end if |
| 090 | #フォルダ名を修正 |
| 091 | set boolDirExists to appFileManager's fileExistsAtPath:(ocidThemesDirPath) isDirectory:(true) |
| 092 | if boolDirExists is false then |
| 093 | log "【警告】:テーマフォルダが見つかりません" |
| 094 | set listDone to appFileManager's createDirectoryAtURL:(ocidThemesDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error|:(reference) |
| 095 | set boolWarning to true as boolean |
| 096 | end if |
| 097 | ########################### |
| 098 | #フォルダ名ローカライズ |
| 099 | set ocidTemplatesDirPathURLdist to ocidBackupDirPathURL's URLByAppendingPathComponent:("Templates.localized") isDirectory:(true) |
| 100 | set ocidStartupDirPathURLdist to ocidBackupDirPathURL's URLByAppendingPathComponent:("Startup.localized") isDirectory:(true) |
| 101 | set ocidThemesDirPathURLdist to ocidBackupDirPathURL's URLByAppendingPathComponent:("Themes.localized") isDirectory:(true) |
| 102 | ########################### |
| 103 | #まずはバックアップ |
| 104 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 105 | set listDone to (appFileManager's copyItemAtURL:(ocidTemplatesDirPathURL) toURL:(ocidTemplatesDirPathURLdist) |error|:(reference)) |
| 106 | set listDone to (appFileManager's copyItemAtURL:(ocidStartupDirPathURL) toURL:(ocidStartupDirPathURLdist) |error|:(reference)) |
| 107 | set listDone to (appFileManager's copyItemAtURL:(ocidThemesDirPathURL) toURL:(ocidThemesDirPathURLdist) |error|:(reference)) |
| 108 | ########################### |
| 109 | #すでに初期ファイルがある場合は |
| 110 | #インストールしない |
| 111 | #Templates |
| 112 | set ocidTemplateFilePathDist to ocidTemplatesDirPath's stringByAppendingPathComponent:("Normal.dotm") |
| 113 | #Startup |
| 114 | set ocidStartupFilePathDist to ocidStartupDirPath's stringByAppendingPathComponent:("Excel/book.xltx") |
| 115 | #Themes |
| 116 | set ocidThemesFilePathDist to ocidThemesDirPath's stringByAppendingPathComponent:("Default Theme.thmx") |
| 117 | ########################### |
| 118 | #配布用のサンプルファイル |
| 119 | set aliasPathToMe to (path to me) as alias |
| 120 | set strPathToMe to (POSIX path of aliasPathToMe) as text |
| 121 | set ocidPathToMeStr to refMe's NSString's stringWithString:(strPathToMe) |
| 122 | set ocidPathToMe to ocidPathToMeStr's stringByStandardizingPath() |
| 123 | set ocidContainerDirPath to ocidPathToMe's stringByDeletingLastPathComponent() |
| 124 | set ocidXLTXFilePath to ocidContainerDirPath's stringByAppendingPathComponent:("Template/book.xltx") |
| 125 | set ocidDOTMFilePath to ocidContainerDirPath's stringByAppendingPathComponent:("Template/Normal.dotm") |
| 126 | set ocidTHMXFilePath to ocidContainerDirPath's stringByAppendingPathComponent:("Template/Default Theme.thmx") |
| 127 | ########################### |
| 128 | #すでに初期設定ファイルがある場合は |
| 129 | #サンプルのファイルをコピーしない |
| 130 | set boolDirExists to appFileManager's fileExistsAtPath:(ocidTemplateFilePathDist) isDirectory:(false) |
| 131 | if boolDirExists is false then |
| 132 | set listDone to (appFileManager's copyItemAtPath:(ocidDOTMFilePath) toPath:(ocidTemplateFilePathDist) |error|:(reference)) |
| 133 | end if |
| 134 | # |
| 135 | set boolDirExists to appFileManager's fileExistsAtPath:(ocidStartupFilePathDist) isDirectory:(false) |
| 136 | if boolDirExists is false then |
| 137 | set listDone to (appFileManager's copyItemAtPath:(ocidXLTXFilePath) toPath:(ocidStartupFilePathDist) |error|:(reference)) |
| 138 | end if |
| 139 | # |
| 140 | set boolDirExists to appFileManager's fileExistsAtPath:(ocidThemesFilePathDist) isDirectory:(false) |
| 141 | if boolDirExists is false then |
| 142 | set listDone to (appFileManager's copyItemAtPath:(ocidTHMXFilePath) toPath:(ocidThemesFilePathDist) |error|:(reference)) |
| 143 | end if |
| 144 | ########################### |
| 145 | #バックアップ先のフォルダを開いて終了 |
| 146 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
| 147 | set boolDone to appSharedWorkspace's openURL:(ocidBackupDirPathURL) |
| 148 | try |
| 149 | with timeout of 5 seconds |
| 150 | tell application "System Events" to quit |
| 151 | end timeout |
| 152 | end try |
| 153 | if boolWarning is true then |
| 154 | set strLOG to ("ユーザーコンテンツのフォルダ名に警告がありました" & return & "ユーザーが意図せずフォルダ名を変更してしまったようです" & return & "ユーザーコンテンツフォルダは『ローカライズ指定』があります" & return & strContentDirPath & return & strTemplatesDirPath & return & strStartupDirPath & return & strThemesDirPath & return & "です。" & return & "ユーザーのファイルを移動してあげてください") |
| 155 | return strLOG |
| 156 | end if |
| 157 | return boolDone |
| 158 | ########################### |
| 159 | #日付情報の取得 |
| 160 | to doGetDateNo(argFormatStrings) |
| 161 | |
| 162 | set ocidDate to refMe's NSDate's |date|() |
| 163 | set ocidNSDateFormatter to refMe's NSDateFormatter's alloc()'s init() |
| 164 | ocidNSDateFormatter's setLocale:(refMe's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX") |
| 165 | ocidNSDateFormatter's setDateFormat:(argFormatStrings) |
| 166 | set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:(ocidDate) |
| 167 | set strDateAndTime to ocidDateAndTime as text |
| 168 | return strDateAndTime |
| 169 | |
| 170 | end doGetDateNo |
| AppleScriptで生成しました | |
