LoginSignup
2
1

[Automator] [AppleScript] [VPN] GlobalProtectの自動ログイン

Last updated at Posted at 2021-08-12

Macの標準アプリ「Automator(オートメーター)」を利用して、GlobalProtect(VPN接続アプリ)へのログインを自動化する

手順

Automator(オートメーター)を起動
00.png

「新規作成」を選び、「書類の選択」では**「アプリケーション」**を選びます。
01.png

左側のアクションの中から**「AppleScriptを実行」を選びます。
以下の
AppleScriptのコード**を以下コードを貼り付けます
04.png

AppleScriptのコード

あなたのユーザー名あなたのパスワードを書き換えてください

--
-- GlobalProtectアプリケーションにログインし、VPNに接続するAppleScriptです。
-- ユーザー名とパスワードはコード内に平文で記述されています。
--

-- ユーザー名とパスワードの設定はコード内に記載する必要があります
property userAccount : "あなたのユーザー名"
property userPassword : "あなたのパスワード"

-- メインハンドラ
on run {}
	-- GlobalProtectアプリケーションを操作する
	try
		-- メニューバーアイテムをクリックする
		clickMenuBarItem()
		
		-- 接続ボタンをクリックする
		clickButton("接続", "Connect")
		
		-- ログイン画面が表示されるまで待機する
		waitForLoginWindow()
		
		-- ユーザー名とパスワードを入力する
		enterCredentials(userAccount, userPassword)
		
		-- Enterキーを押して接続する
		pressEnterKey()
	on error errMsg
		display alert "An error occurred: " & errMsg
	end try
	
	return
end run

-- メニューバーアイテムをクリックする
on clickMenuBarItem()
	tell application "System Events" to tell process "GlobalProtect"
		click menu bar item 1 of menu bar 2
	end tell
end clickMenuBarItem

-- 指定されたUIエレメントがウィンドウ内に存在する場合、クリックする
on clickButton(japaneseLabel, englishLabel)
	tell application "System Events"
		if exists UI element japaneseLabel of window 1 of application process "GlobalProtect" then
			click UI element japaneseLabel of window 1 of application process "GlobalProtect"
		else if exists UI element englishLabel of window 1 of application process "GlobalProtect" then
			click UI element englishLabel of window 1 of application process "GlobalProtect"
		end if
	end tell
end clickButton

-- ログイン画面が表示されるまで待機する
on waitForLoginWindow()
	tell application "System Events"
		repeat until (exists UI element "キャンセル" of window 1 of application process "GlobalProtect") or ¬
			(exists UI element "Cancel" of window 1 of application process "GlobalProtect") or ¬
			(exists UI element "接続" of window 1 of application process "GlobalProtect") or ¬
			(exists UI element "Connect" of window 1 of application process "GlobalProtect")
			delay 0.5
		end repeat
	end tell
end waitForLoginWindow

-- ユーザー名とパスワードを入力する
on enterCredentials(account, passwd)
	-- ユーザー名をクリップボードからペーストする
	set the clipboard to {text:(account as string), Unicode text:account}
	tell application "System Events"
		keystroke "v" using {command down}
		delay 0.3
	end tell
	
	-- Tabキーを押す
	pressTabKey()
	
	-- パスワードを入力する
	typePassword(passwd)
end enterCredentials

-- Tabキーを押す
on pressTabKey()
	tell application "System Events"
		keystroke (ASCII character 9)
	end tell
end pressTabKey

-- パスワードを入力する
on typePassword(passwd)
	-- パスワードをクリップボードからペーストする
	set the clipboard to {text:(passwd as string), Unicode text:passwd}
	tell application "System Events"
		keystroke "v" using {command down}
	end tell
end typePassword

-- Enterキーを押す
on pressEnterKey()
	tell application "System Events"
		keystroke (ASCII character 13)
	end tell
end pressEnterKey

最後に名前をつけて保存します
03.png
GlobalProtect自動ログインアプリの完成です

Automatorのアイコンを変える

右クリックして「情報を見る」をクリックします。
左上のロボットアイコンをクリックして選択(縁が青くなります)
002.png

アイコン画像 をクリップボードへコピーし、ペースト(⌘Command+V)
001.png

#編集方法(パスワードの更新時など)
Automator(オートメーター)を起動し、作成したGlobalProtect自動ログインアプリを選択
AppleScriptのコードを編集し、保存(⌘Command+S)します

0000.png

2
1
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
2
1