LoginSignup
1
0

NASのフォルダをマウントしてChromeで管理コンソールを開くAppleScript

Last updated at Posted at 2024-03-10

NASを購入したが、MacのSMB接続が不安定なのと、管理コンソールを簡単に開けるようにAppScriptを使ってアプリを作ってみたのでメモがてら書いておく

AppleScriptは、いろいろとクセがあって、他のプログラミング言語ではあたりまえの書き方ができずになかなか大変だったが、Macの操作ができるアプリケーションが作れるので、作り方がわかっていると、仕事やプライベートでMacでの作業効率化をあげたいときの選択肢に十分なり得るので、個人的にはよかった。

とりあえず、スクリプトだけ置いて、細かい説明は時間があるときに、補足する

aaa
set serverName to "[NASの名前]"
set consoleUrl to "[NASの管理コンソールのURL]"

set userName to short user name of (system info)
set userInfo to "アカウント:" & tab & userName
log userName

try
	set userPassword to do shell script "security find-internet-password -s " & serverName & " -w"
on error
	set title to "パスワードの取得失敗"
	set stitle to userInfo
	display notification title with title title subtitle stitle sound name "Frog"
	display dialog title & return & "キーチェーンの設定を確認してください。" with title "失敗" buttons {"OK"} default button 1 with icon 2
	return
end try

set smbURIFormat to "smb://user:password@server"
set smbURIs to {}
copy replaceText(smbURIFormat & "/home", "server", serverName) to the end of smbURIs
copy replaceText(smbURIFormat & "/share", "server", serverName) to the end of smbURIs
try
	repeat with smbURI in smbURIs
		mount volume replaceText(smbURI, "user:password@", userName & ":" & userPassword & "@")
	end repeat
on error
	set title to "ネットワークフォルダのマウント失敗"
	set stitle to userInfo
	set message to joinList(smbURIs, return)
	display notification message with title title subtitle stitle sound name "Frog"
	display dialog title & return & "しばらくしてから実行してください。" & return with title "失敗" buttons {"OK"} default button 1 with icon 2
	return
end try

set title to "ネットワークフォルダのマウント成功"
set stitle to userInfo
set message to joinList(smbURIs, return)
display notification message with title title subtitle stitle sound name "Blow"
display dialog title & return & "管理コンソールを開きますか?" with title "成功" buttons {"いいえ", "はい"} default button 2 with icon 1 giving up after 10
set tmp to result
set btn to button returned of tmp
set gu to gave up of tmp

if btn = "はい" then
	tell application "Google Chrome"
		make new window
		activate
		tell front window
			tell active tab
				set URL to consoleUrl
			end tell
		end tell
	end tell
else
	return
end if

on replaceText(theText, serchStr, replaceStr)
	set tmp to AppleScript's text item delimiters
	set AppleScript's text item delimiters to serchStr
	set theList to every text item of theText
	set AppleScript's text item delimiters to replaceStr
	set theText to theList as string
	set AppleScript's text item delimiters to tmp
	return theText
end replaceText

on joinList(theList, aDelimiter)
	set tmp to AppleScript's text item delimiters
	set AppleScript's text item delimiters to aDelimiter
	set theText to theList as string
	set AppleScript's text item delimiters to tmp
	return theText
end joinList

参考にしたサイト

AppleScript 最速基本文法マスター
文字列の検索・置換
AppleScript ListとRecordの違い
AppleScript の基本文法
AppleScript : display dialog の表示と返り値
Displaying Notifications法
AppleScript で Google Chrome を操作してウェブサイトを開くコード

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0