You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript that will highlight 32 characters?

Hi there, I'm hoping to streamline my video-captioning efforts that I am doing in various web-based applications.


Video captions should be limited to two lines of 32 characters or less. When I am parsing out some Auto-generated captions, I will place my cursor at the beginning of the caption text, hit the right-arrow key 32 times. This allows me to see where I need to place a line-break in the captions, or create a new caption entirely.


I'm new to AppleScripting and am hoping someone could help me write a script that would take the current cursor position, hold down the shift key and hit the right arrow 32 times, so that I can visually see the text that gets selected via this script.


Any help is greatly appreciated!

Posted on May 14, 2020 10:31 AM

Reply
Question marked as Top-ranking reply

Posted on May 20, 2020 3:48 PM

Thank you for your help! I think I'm close!


  • I opened Automator and chose Quick Action as the document type.
  • I chose "Run AppleScript" from the Actions, kept the default values of the fields (Workflow receives, ..., Color), and pasted your block of code in the text field, replacing the "(* Your script goes here *)". So the code looks like:


on run {input, parameters}

	tell application "System Events"
		repeat 32 times
			key code 124 using {shift down}
		end repeat
	end tell

	return input
end run


  • I saved this Quick Action as "Quick Action - Highlight 32 Characters".
  • Next, I went to System Preferences > Keyboard > Shortcuts > and found " Quick Action - Hig...ight 32 Characters" listed under Text. I set shortcut to ⌃→ (control and the right arrow)
  • When prompted to allow automator to control my computer, I entered my password.
  • I went back to my browser, hit ⌃→, and nothing happened.


What am I missing?


Thanks again for your help and patience!

Similar questions

8 replies
Question marked as Top-ranking reply

May 20, 2020 3:48 PM in response to Camelot

Thank you for your help! I think I'm close!


  • I opened Automator and chose Quick Action as the document type.
  • I chose "Run AppleScript" from the Actions, kept the default values of the fields (Workflow receives, ..., Color), and pasted your block of code in the text field, replacing the "(* Your script goes here *)". So the code looks like:


on run {input, parameters}

	tell application "System Events"
		repeat 32 times
			key code 124 using {shift down}
		end repeat
	end tell

	return input
end run


  • I saved this Quick Action as "Quick Action - Highlight 32 Characters".
  • Next, I went to System Preferences > Keyboard > Shortcuts > and found " Quick Action - Hig...ight 32 Characters" listed under Text. I set shortcut to ⌃→ (control and the right arrow)
  • When prompted to allow automator to control my computer, I entered my password.
  • I went back to my browser, hit ⌃→, and nothing happened.


What am I missing?


Thanks again for your help and patience!

May 14, 2020 10:49 AM in response to jeremyfromtacoma

At a high level this is pretty easy, although I suspect there's a lot more to this:


tell application "System Events"
	repeat 32 times
		key code 124 using {shift down}
	end repeat
end tell


From an activation standpoint, it's probably easiest to add this in a 'Run AppleScript' action in an Automator workflow. This can be saved as a service, which you can then subsequently allocate a command-key via System Preferences -> Keyboard.

May 15, 2020 9:32 AM in response to jeremyfromtacoma

The script blindly sends the keystrokes. It has no concept of which application is frontmost and will receive the keys.


As for it working in a web browser, it does work for me if I make sure a) the web browser is frontmost and b) I'm in an editable text file when the script runs. Ordinarily the right arrow key doesn't do anything on most web pages.


You may need to add additional code to the script to make sure the keys are going where you expect/want them to. I don't know your use case to be able to add this blindly, but this totally falls into the 'suspect there's a lot more to this' category :)

May 20, 2020 11:41 AM in response to Camelot

Here's a quick video capture I did to show you what I'm manually doing within TechSmith Relay:


https://nic.techsmithrelay.com/rWzN


Again, being able to push a shortcut button on my keyboard to run an AppleScript that would hold shift and hit the right arrow-key 32 times would help me work through captions so much faster and with better accuracy.


Many thanks for your help!

May 20, 2020 1:28 PM in response to jeremyfromtacoma

I get what you're trying to do - and maintain that my script will do this. What's missing is how to invoke the script.


If you want to assign a keystroke to it, then your simplest solution is to use Automator to create a Service/Quick action script. This workflow has a single 'Run AppleScript' action with my code, above.

You can use this to limit to working in specific apps (or all apps), and once it's saved you can assign a keyboard command to it via System Preferences -> Keyboard -> Shortcuts -> Services, or add it to your Touch Bar (if your system has one) via System Preferences -> Extensions -> Touch Bar

AppleScript that will highlight 32 characters?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.