Skip to content

Add toggle-fn extension - #13942

Merged
raycastbot merged 5 commits into
raycast:mainfrom
elonwoo:ext/toggle-fn
Aug 25, 2024
Merged

raycastbot merged 5 commits into
raycast:mainfrom
elonwoo:ext/toggle-fn

Conversation

@elonwoo

@elonwoo elonwoo commented Aug 9, 2024 •

Copy link
Copy Markdown
Contributor

Description

Toggle the "Use all F1, F2, etc. keys as standard function keys" option in macOS System Preferences.

Screencast

Toggle-Fn

Checklist

@raycastbot

Copy link
Copy Markdown
Collaborator

Congratulations on your new Raycast extension! 🚀

You can expect an initial review within five business days.

Once the PR is approved and merged, the extension will be available on our Store.

@raycastbot raycastbot added the new extension Label for PRs with new extensions label Aug 9, 2024
@pernielsentikaer

Copy link
Copy Markdown
Contributor

Thanks for your contribution 👋

It works as expected as long as you don't change the language. I tried to switch to my native language, Danish, and it stops working there

Raycast16082024-bWT0Rdhs png@2x

Can you come up with a way that works for other languages too?

@pernielsentikaer pernielsentikaer self-assigned this Aug 16, 2024
@elonwoo

elonwoo commented Aug 19, 2024

Copy link
Copy Markdown
Contributor Author

Thanks for your contribution 👋

It works as expected as long as you don't change the language. I tried to switch to my native language, Danish, and it stops working there

Raycast16082024-bWT0Rdhs png@2x

Can you come up with a way that works for other languages too?

Sorry, I know what the problem is, but it is difficult for me to adapt this script to so many languages ​​without the corresponding API provided by Mac.

I am not able to solve this problem at the moment, and I hope that there will be contributors in the community to improve this extension.

@pernielsentikaer

@pernielsentikaer

Copy link
Copy Markdown
Contributor

Maybe we can tweak it, this seems to work with other language, but it doesn't close the window, maybe you can fix that? It toggles in checkbox at least 🙂

set osver to system version of (system info)
if osver ≥ 13.0 then
	open location "x-apple.systempreferences:com.apple.Keyboard-Settings.extension"
	delay 1 -- Wait for the settings to open
	
	if osver ≥ 14.0 then
		tell application "System Settings"
			reveal anchor "FunctionKeys" of pane id "com.apple.Keyboard-Settings.extension"
		end tell
	else
		tell application "System Events" to tell process "System Settings"
			repeat until window begins with "Keyboard" exists
				delay 0.5
			end repeat
			
			repeat until exists of (1st window whose value of attribute "AXMain" is true)
				delay 0.5
			end repeat
			
			repeat until exists group 1 of group 2 of splitter group 1 of group 1 of window 1
				delay 0.5
			end repeat
			
			set keyboardButton to 3
			try
				click button keyboardButton of group 2 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1
			on error errorMessage number errorNumber
				if errorNumber is -1719 then
					set keyboardButton to 1
					click button keyboardButton of group 2 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1
				end if
			end try
			
			repeat until sheet 1 of window 1 exists
				delay 0.5
			end repeat
			
			keystroke "f"
		end tell
	end if
	
	tell application "System Events" to tell process "System Settings"
		repeat until exists (checkbox 1 of group 1 of scroll area 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window 1)
			delay 0.5
		end repeat
		
		click checkbox 1 of group 1 of scroll area 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window 1
		
		repeat
			try
				repeat while sheet 1 of window 1 exists
					delay 0.5
				end repeat
				exit repeat
			end try
		end repeat
	end tell
	
	tell application "System Settings" to quit
else if osver < 13.0 then
	tell application "System Settings"
		set current pane to pane "com.apple.preference.keyboard"
	end tell
	
	tell application "System Events"
		if UI elements enabled then
			tell application process "System Preferences"
				repeat until exists tab group 1 of window "Keyboard"
					delay 0.5
				end repeat
				click radio button 1 of tab group 1 of window "Keyboard"
				try
					click checkbox 1 of tab group 1 of window "Keyboard"
				end try
				try
					click checkbox 2 of tab group 1 of window "Keyboard"
				end try
			end tell
			tell application "System Settings" to quit
		else
			tell application "System Settings"
				activate
				set current pane to pane "com.apple.preference.security"
				display dialog "UI element scripting is not enabled. Please activate this app under Privacy -> Accessibility so it can access the settings it needs."
			end tell
		end if
	end tell
end if

@elonwoo

elonwoo commented Aug 24, 2024 •

Copy link
Copy Markdown
Contributor Author

@pernielsentikaer

I fixed it based on your code, thank you.

My macOS version is 14.3.1.

I tested in these languages and works well.

  • English
  • Mandarin Chinese
  • Danish
  • Spanish
  • Hindi
  • Arabic
  • French
  • Portuguese
  • Japanese
  • Korean

@pernielsentikaer pernielsentikaer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to work 🔥

The icon is hard to see in dark mode, can you provide alternative dark mode image or make another one?

@elonwoo

elonwoo commented Aug 25, 2024

Copy link
Copy Markdown
Contributor Author

@pernielsentikaer added a dark icon. Thank you for taking the time to check.

image

@pernielsentikaer pernielsentikaer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi 👋

Looks good to me, approved 🔥

@raycastbot
raycastbot merged commit 0a508d9 into raycast:main Aug 25, 2024
@github-actions

Copy link
Copy Markdown
Contributor

@raycastbot

Copy link
Copy Markdown
Collaborator

🎉 🎉 🎉

We've rewarded your Raycast account with some credits. You will soon be able to exchange them for some swag.