Automator: Insert Transformed Text from Clipboard

Dec 11, 2022

I’m migrating a multi-module Android project from the package="at.xa1.…"-notation in AndroidManifest.xml to the new namespace "at.xa1.…" notation in build.gralde. When having many modules, it’s a very boring and cumbersome task. Also it’s not easy to automate. However, at least typing down the new lines in build.gradle can be automated:

android {
    namespace "at.xa1.product.feature.example"
    testNamespace "at.xa1.product.feature.example.test"
}

What I do is use Android Studio’s Full-Text-Search to find all remaining occurrences of package="at.xa1., then I process them one by one:

  1. Use ++ to select the full package name and copy it.
  2. Either remove the package attribute (if there is more content in the AndroidManifest.xml) or delete the entire file if that’s the only content.
  3. Go to the module’s build.gradle, and press my magic custom shortcut ++B, which types:
    namespace "{PACKAGE_IN_CLIPBOARD}"
    testNamespace "{PACKAGE_IN_CLIPBOARD}.test"
    
  4. Repeat with the next find result.

Setup Automator

1 Start Automator

I use +Space and type “Automator” to start it.

2 Create a New "Quick Action"

I will ask you to create a new “document”. Choose “Quick Action”.

After clicking “Choose”, press +S to save it. I called mine “namespace-replace-test”.

Note: The document will be placed in /Users/$USER/Library/Services/namespace-replace-test.workflow

3 Build Workflow

Mine looks like this:

A few details are essential:

  • Make sure “Workflow receives” has “no input” selected.
  • “Output replaces selected text” is unchecked.
  • “Run Shell Script” has “Pass input” set to “as arguments”.

The scripts for copy&paste:

echo "namespace \"$1\""
printf "\r\n"
echo "testNamespace \"$1.test\""
on run {input, parameters}
	
	tell application "System Events" to keystroke "" & input
	
	return input
end run

4 Setup Shortcut

  • Open “System Preferences ▸ Keyboard”.
  • Select the “Shortcuts” tab.
  • Select “Services” in the left list.
  • Your Automator workflow should show up in the right list.
  • Tick it and assign a Shortcut to it.

5 Privacy Settings

Typing into an application (with tell application "System Events" to keystroke …) only works if the receiving application is added to “Accessibility” in the System Settings. If you didn’t add it, you’d receive this error:

  • Open “System Preferences ▸ Security & Privacy”.
  • Select the “Privacy” tab.
  • Select “Accessibility” in the left list.
  • Add the receiving app to the list (in my case: “Android Studio”)

6 Done

You’re now able to copy a package name, press your shortcut, and the Automator will type the text into Android Studio!

Avoid Accessibility Permission

Automator offers the option “Output replaces selected text”, which would be ideal. That way, we can avoid the Accessibility permission and the entire Apple Script with tell application "System Events" to keystroke …. Unfortunately, I couldn’t get it to work with Android Studio. It works for some other Apps (e.g., TextEdit), but for Android Studio, I got this error:

I am still determining the issue with Android Studio precisely, but the workaround with the Apple Script above works for me.

Conclusion

This introduction helps you build your own shortcut base automation tool! After a few minor troubles, it’s very straightforward. Enjoy!

You might also like