20260321

【macOS】Microsoft Office For macOS 新規書類作成時のデフォルト設定


【macOS】Microsoft Office For macOS 新規書類作成時のデフォルト設定

スクリプト単体では動作しません
試したい場合は
こちらからダウンロードしてください


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

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