Run Applescript on selected text and store it on clipboard - applescript

I'm looking for a solution to be able to select a text on an app(Figma) and then reverse it then store reverse version to the clipboard.
I used BetterTouchBar app for saving selected text but I can't do anything with it.
How can I do that with or without BetterTouchbar App.
Thanks a lot.

Create an Automator Quick Action1, setting Workflow receives current text in any application and add a Run AppleScript action, adding the following line of example AppleScript code where is says: (* Your script goes here *)
set the clipboard to the (reverse of characters of item 1 of input) as text
Save the Quick Action as e.g.: Reverse Selected Text To Clipboard
It's now available from the Services menu in any application, e.g. TextEdit > Services, or the Services menu on the Context menu (right-click menu).
You can also assign a keyboard shortcut in: System Preferences > Keyboard > Shortcuts > Services
1 In versions of macOS prior to Mojave a Quick Action is called a Service.
Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the used to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.

Related

How to modify MacOS Dock shortcuts/hotkeys?

I want to modify/change/add MacOS Dock shortcuts/hotkeys.
e.g., of a shortcut that is available by default:
Option-Click on Dock app icon of an app that is not currently open = Hide the currently active app and then Open the app that was clicked
(from: https://support.apple.com/kb/PH21922?locale=en_US)
One very specific ability that I want:
Open and then Hide an app
Shift-Click on Dock app icon = Open and then Hide that app
(or use another easy modifier-key-combo with the click)
I am aware of the bash command open -a App --hide (e.g., open -a TextEdit --hide). I want to implement this exact functionality with a convenient Dock shortcut like the one mentioned above. If you're wondering "why?": sometimes I just want to open an app because I know that I will need it soon, but I'm still busy with another app, so just open this second app and then immediately hide it so it doesn't get in my way while I'm still busy with that first app.
How do I do this?
You can't.
These keyboard bindings are built into the Dock application, and cannot be modified.
The Mac utility program, Keyboard Maestro, does what you've asked, with shortcut keys, without using the Dock:
open, then immediately hide/minimize
option+open
I came up with a related solution: (in case anyone is interested)
an AppleScript App that presents a pick list
Method:
create a plain text document containing a list of the apps you want to handle (use correct name, no path, no extension, one name per line, no commas)
open 'Script Editor' (/Applications/Utilities)
copy-pasta the following code (and edit the first code line for the path to your text file from first step)
set apps_file to ("path:apps_list.txt")
set apps_list to paragraphs of (read file apps_file)
set apps_pick to choose from list apps_list with prompt "Select one or more apps." with multiple selections allowed
if result is false then return
set path_base to "Macintosh HD:Applications:"
set path_msft to path_base & "Microsoft Office 2011:"
set path_utly to path_base & "Utilities:"
set spec_msft to "Microsoft"
set spec_utly to "Activity Monitor, Terminal"
repeat with apps_this in apps_pick
if apps_this contains spec_msft
set path_this to path_msft
else if apps_this is in spec_utly
set path_this to path_utly
else
set path_this to path_base
end if
set apps_open to path_this & apps_this & ".app"
run application apps_open
end repeat
return
i. navigate menu 'File -> Export...'; ii. use the option 'File Format: Application'; iii. Save; (put the resulting app in your Dock)
Its not quite as convenient as I hoped, but, not too shabby.

Xcode, default window layouts?

No-one has been able to answer this question.
It would seem to be impossible to have XCode open this way.
However Youssef provided the most useful answer - so Youssef gets the points, thank you Youssef.
Note for future readers .. the mac utility Moom is excellent for some, but not all, problems of this nature. Again it is not a total solution.
Using the current up-to-date XCode,
Whenever I OPEN a project in XCode (or start a new project),
it looks like this:
However, I want it to look like this:
Again that's when I OPEN a project.
Is there any way to achieve this? Thanks.
Go to Xcode (in the top menu bar) --> Preferences...
Switch to the Behaviors tab and modify what you want in the Running section
You'll get something like this:
You can choose to show/hide what you need, and specify the default behavior
Cheers
EDIT:
After understanding what you need, I will modify my answer.
There is no way to force XCode to automatically enter a behavior at startup.
The best you can do is to create a custom behavior and give it a keyboard shortcut. And when you start XCode just use the keyboard shortcut to enter the desired behavior.
This is how it is done:
In the behaviors tab (above) click on the + sign at the bottom
Then enter the desired name of this behavior (Maybe StartupBehavior)
Click on the left button to modify the shortcut key. My preferred shortcut key is Command + ` (the button to the left of the '1' key). This is because it will not override any other command and it is easy to click.
Note: you can specify any shortcut you like and dont be afraid to override one that already exists if you don't use it.
Now you need to configure your behavior the way you like it
Close the preference windows.
Now everytime you start XCode just use the shortcut key you specified (Command + ` in my case) to quickly load your behavior
Note:
If you dont like keyboard shortcuts you can also load your behavior by going to Xcode (menu bar) --> Behaviors --> Select your behavior
That is the quickest way you can achieve what you want. I dont think you will be able to force Xcode to run your behavior automatically on startup.
Hope it helped.
Cheers
(not really default layout, you have to hit at least 2 keys simultaneously once XCode is open) You could set up a behavior for "Build->Starts" and hit "CMD + B"
after opening XCode / any Project. This way it will 1. change to your
Layout even if there are any errors and Build fails and 2. you don't
have to Stop it again.
As already said, it's not a default layout, but I like to let the compiler run through projects of other people at the beginning anyway.
You could search for some Keys in XCodes preference file in
~/Library/Preferences/com.apple.Xcode.plist as already suggested in
my comment, I'm not experienced enough to know which are the right
ones, sorry.
Wrap the .xcodeproj File opening in an application. E.g. in no way "clean", but anyway:
Set up an XCode behavior like in option 1
Open Automator, choose Application, drag "open Finder Items" and "run AppleScript" inside.
on run {input, parameters}
set frontmostAppName to name of (info for (path to frontmost application))
repeat while frontmostAppName is not "XCode.app"
set frontmostAppName to name of (info for (path to frontmost application))
delay 0.5
end repeat
try
tell application "System Events" to keystroke "b" using {command down} --simulates CMD + B
end try
return input
end run
Insert some applescript like the above and save the Automator application.
RightClick -> getInfo on any .xcodeproj File in Finder/on Desktop, in "open with" select your new Automator app, click add and back in the info panel -> check "Change All"
You can obviously create a new Behavior instead of using Build->starts, assign a different Hotkey in XCode and script e.g. tell application "System Events" to keystroke "i" using {command down, control down, shift down, alt down} , optimize the applescript somehow or do everything via a real cocoa app if you want
Well, you may be able to approach it using preconfigured xcuserstate files nested in your project templates' default workspace directory, but that is going to be a pain to tweak and maintain. I'm not even sure it would work.
I would personally just approach it by using Behaviors.
So:
1) create a Behavior
2) Give it a convenient key binding
3) Then define the Behavior. For example:
ASCII Behavior Editor:
...
√ Show *Debugger* with *Console View*
√ *Hide* utilities
...
That should do it. Just hit the assigned key command when you want to restore that display state, regardless of its initial state.
Of course, you can do a few other things or all sorts of arbitrary stuff by running an external AppleScript (triggered with a Behavior's run script facility).

Have one application execute an action when another application is given a command using AppleScript?

I know nothing about AppleScript, but I wonder if it could make my life easier: is there a way to write an AppleScript that tells Safari / Firefox / Chrome to refresh the current tab when I save a document in another application, say TextWrangler? Essentially, I want to map the Command+S keyboard shortcut to do two things at once in two separate applications.
If that’s not possible, can you script one application so that saving one file executes a command in another window in that same application?
There are different possible approaches to implement this, but the most straightforward would probably be to create a script that executes all steps you need (i.e. save the document and refresh the window) and bind that to the Cmd+S keyboard combo in the triggering application.
What you need for this approach to work, is, in order:
a method to bind a key combo to a script effective only in a specific application. OS X’ Automator Services fit that bill: their scope can be restricted to a single application (select it in the “only in” drop down at the top of the workflow actions), and they can be assigned a shortcut in the Keyboard preference pane of System Preferences.
a way to relay your commands to the applications they target. AppleScript can help you in two different ways, depending on the fact if your applications are scriptable, i.e. if they have a scripting dictionary you can inspect in the AppleScript editor:
if they do, and their terminology includes the saving action for the editor on one side (most scriptable document based apps do so in the form save <document>), the page refreshing for the browser(s) on the other (Chrome has reload <tab>, Safari gets the same result via a JavaScript detour, i.e. do JavaScript "window.location.reload()" in <document> – I don’t use Firefox), you are set.
if they do not, GUI Scripting might help, i.e. simulating a click on the right UI element (menu or toolbar) via tell application "System Events" to tell process <your process> to click item x of menu y.
That script can then be embedded into the Automator workflow (in an “Run AppleScript” action, to be precise).
As you can see, a lot depends on the exact software you are using. if you are new to AppleScript and the above baffles you, I’d suggest spending a bit of time on the AppleScript pages of Mac OS X Automation (where you’ll also find example scripts which will kick start you into things like GUI Scripting).
One final note: as of this writing, sandboxed applications do not honor key combos assigned to them in the Keyboard Preference pane (they do honor global key combos set there – just not those specifically targeting them). This means you cannot, for instance, currently override TextEdit’s Cmd+S shortcut for saving in Lion. As long as your editor is not sandboxed (easily checked in Activity Monitor), you should have no issue with this.
One solution would be to create a folder action to refresh the current tab when a new file is saved in the folder.
on adding folder items to theFolder after receiving theFiles
tell application "Google Chrome"
activate
tell window 1 to tell active tab
set URL to (get URL)
end tell
end tell
end adding folder items to

Applescript Droplet Text as Input

How can I create a droplet that takes a text selection as input? When I create a script that starts with on run inputText, the resulting application icon will only darken when files are dragged over it.
You can achieve a similar result by using Automator to make a service. Services can be fed selected text, (or urls or files etc) and not just from Finder, but from the right-click contextual menu or the Services menu. You can run applescript inside the Automator script, so basically Automator makes a wrapper for your appleScript. The downside is that it tends to be even slower than applescript.
Dropplets in AppleScript only support files. You can follow #stib's suggestion of using a service with Automator or using the Scripts menu (launch AppleScript Editor and choose AppleScript Editor>Preferences from the menu bar, General in the preferences window and check "Show Script menu in menu bar"). You can then place the script in the /Library/Scripts/ or ~/Library/Scripts folder to have the script appear in the menu. Alternatively, check out FastScripts to include the ability to assign keyboard shortcuts to the scripts and enhanced menu organization.
In applescript, you can create a simple droplet like this:
on open theThing
set fileToRead to open for access theThing --open the file so we can perform operations on it
set myVar to (read fileToRead) --The myVar variable is set to the contents of the dropped file
display dialog myVar --Shows the contents of the file in a dialog; do what you want with the text here
//other code here
close access fileToRead
end open
So, it's not too hard, just make sure you open for access the file first. I hope this helped!
Helpful Links:
http://macscripter.net/viewtopic.php?id=24772: About Droplets
http://macscripter.net/viewtopic.php?id=24745: About File IO
As far as I could tell, this could only be achieved by wrapping the Applescript in a Cocoa application. I don't know Objective-C, but was able to cobble something together. When I get a chance I'll try to clean up a bit and post an explanation.

AppleScript Editor record doesn't work

I have opened the AppleScript Editor and pressed Record button.
Then I run TextEdit, create a file and put some text there.
When I click the Stop button in AppleScript Editor, nothing was recorded, the window is blank.
What is the problem?
You can use the Record feature of the Automator to record the UI interaction steps needed to do the relevant workflow. Then you can then literally select and copy the recorded steps in automator and paste them into a new Applescript Editor window. This will give you applescript which may or may not work. You'll probably want/need to edit the resulting script, but at least it should help give an idea what is needed to achieve your workflow programatically. This method is usable regardless of whether or not the target application has an applescript dictionary or supports the AppleScript Editor Record button, as it is the interaction with the underlying UI elements which is recorded.
Steps:
Open Automator
Start a new "Workflow"
Start recording
Perform whatever steps you require with your app (in this case typing into textedit)
Stop recording
This will create a list of actions in Automator like:
Select all these and copy (CMD+c)
Open the Applescript Editor app
Paste (CMD+v). The result will be valid applescript to perform the actions you just recorded:
Note that as is generally the case with UI automation, the automator records steps exactly and the script plays them back exactly. This my not be exactly what you want - e.g. if a different application were active, the text could get typed in there instead. The generated applescript should be used as a guide to the final applescript.
The problem is that applications need to explicitly support AppleScript recording in order for it to work, but almost no applications actually do. Finder still supports it a bit, and maybe a couple other apps (BBEdit comes to mind), but for the most part, AppleScript recording has been pretty useless for quite some time.
Not all apps are recordable (in fact, only a small handful are). Recordablity is something each app needs to implement, and I guess TextEdit isn't recordable.

Resources