#!/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 strFilePath to ("~/Library/LaunchAgents/com.dropbox.DropboxMacUpdate.agent.plist")
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 ocidFilePath to doChkExists(ocidFilePathURL)
log ocidFilePath as text
####################################
#上書きチェック
# ocid file path = NSPathStore を返します
####################################
to doChkExists(argFilePath)
log (className() of argFilePath) as text
if (class of argFilePath) is text then
log "テキストファイルパス"
set ocidFilePathStr to refMe's NSString's stringWithString:(argFilePath)
set ocidArgFilePath to ocidFilePathStr's stringByStandardizingPath()
else if (class of argFilePath) is alias then
log "エリアスファイルパス"
set strArgFilePath to (POSIX path of argFilePath) as text
set ocidFilePathStr to refMe's NSString's stringWithString:(argFilePath)
set ocidArgFilePath to ocidFilePathStr's stringByStandardizingPath()
else if (class of argFilePath) is «class furl» then
log "エリアスfurlファイルパス"
set aliasFilePath to argFilePath as alias
set strArgFilePath to (POSIX path of argFilePath) as text
set ocidFilePathStr to refMe's NSString's stringWithString:(argFilePath)
set ocidArgFilePath to ocidFilePathStr's stringByStandardizingPath()
else if (className() of argFilePath as text) contains "NSCFString" then
log "NSStringファイルパス"
set ocidArgFilePath to argFilePath's stringByStandardizingPath()
else if (className() of argFilePath as text) contains "NSPathStore" then
log "NSPathStore2ファイルパス"
set ocidArgFilePath to argFilePath
else if (className() of argFilePath as text) contains "NSURL" then
log "NSURLファイルパス"
set ocidArgFilePath to argFilePath's |path|
end if
####
set appFileManager to refMe's NSFileManager's defaultManager()
set boolExists to appFileManager's fileExistsAtPath:(ocidArgFilePath) isDirectory:(false)
#
if boolExists = true then
##ダイアログを前面に
set strName to (name of current application) as text
if strName is "osascript" then
tell application "Finder" to activate
else
tell current application to activate
end if
set strMes to "上書きします?" as text
try
set objResponse to (display alert strMes buttons {"上書きする", "処理を中止する", "ファイル名を変更"} default button "上書きする" cancel button "処理を中止する" as informational giving up after 20)
on error
log "処理を中止しました"
return "処理を中止しました"
error number -128
end try
if true is equal to (gave up of objResponse) then
log "時間切れですやりなおしてください"
return "時間切れですやりなおしてください"
error number -128
end if
if "上書きする" is equal to (button returned of objResponse) then
log "上書き保存します"
set ocidReturnFilePath to ocidArgFilePath
else if "ファイル名を変更" is equal to (button returned of objResponse) then
log "ファイル名を変更"
set ocidContainerDirFilePath to ocidArgFilePath's stringByDeletingLastPathComponent()
set strFileName to ocidArgFilePath's lastPathComponent() as text
set aliasContainerDirPath to (POSIX file (ocidContainerDirFilePath as text)) as alias
##
set strPromptText to "名前を決めてください" as text
set strMesText to "名前を決めてください" as text
###ファイル名 ダイアログ
set aliasFilePath to (choose file name strMesText default location aliasContainerDirPath default name strFileName with prompt strPromptText) as «class furl»
set strFilePath to (POSIX path of aliasFilePath) as text
set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
set ocidReturnFilePath to ocidFilePathStr's stringByStandardizingPath()
else if "処理を中止する" is equal to (button returned of objResponse) then
return "処理を中止しました"
else
return "エラーしました"
error number -128
end if
else if boolExists = false then
log "そのままファイル生成"
set ocidReturnFilePath to ocidArgFilePath
end if
return ocidReturnFilePath
end doChkExists