Applescript Droplet Text as Input - applescript

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.

Related

Shell script to create new file on right click for macOS

I would like to add a context menu item in MacOS Mojave which gives the option to create a new file in the finder on a right click (the way you would in windows). I would also like to be able to specify the file type (text, word, html, css, javascript, etc)
I have followed a tutorial online in order to create the context menu item with automator but I am not familiar with writing shell scripts.
is it just as simple as:
for f in "$#"
do
touch "$f"
done
I was able to implement this following the instructions here: How to get Create New Text File from context menu
The only drawback as it stands is that you need to select a folder in finder to get the context menu. I would like to be able to click in the current I am in and get the menu but I can live with this. Hope this helps someone else.
This tool provides the "Create a file here" option out of the box. With this tool you can easily create your own options as well by writing simple shell scripts. The tool is free and the page has a link to the source if you want to check the source. The source has a shell script that creates a file in a directory that is given as an argument to the script. The script avoids file name conflicts when creating new files.

convert simple automator application to applescript

I am very new to both automator and applescript. I have created an application that uses automator to print file(s) that are dragged and dropped onto the program icon. it is very simple. In automator, I just run
Print Finder Items
and the program works. Now I need to do more complicated things and I need to figure out how to convert that one command into applescript. I've tried the advice given in multiple stack overflow articles including: Automator to Applescript for Editing Code but it does not help me. How do I convert this to applescript?
tell application "Finder"
set theItems to selection
print theItems
end tell
You need to learn how to find the commands yourself so you can explore what they do. Open AppleScript Editor. Under the File menu choose "open dictionary" and select the Finder. This dictionary tells you everything you can do with applescripting the Finder. Each application has its own dictionary so you need to start looking at these.
That script works on any files you select in the Finder. Good luck.

AppleScript to paste text from clipboard into a file

I thought this would be easy. The Google makes me think otherwise.
What I want is a poor man's inter-OS clipboard. Everything I looked at on the net is either not free, no app this simple should cost anybody anything, or isn't compatible with local linux installs or Windows or some such.
In the best solution a right mouse action would be added to "cut", "copy" and "paste" named something like "copy to file". The file might or might not exist but would have a fixed name and be on a shared disk.
I guess I'd need a second right mouse action "Paste from file" to complement the "copy to file".
So, would some one show me how to have an AppleScript or, maybe, Automator, take the current text contents of the clipboard and paste into an existing file, overwriting any
existing contents of the file?
OS/X Snow Leopard
Thanks.
how to have an AppleScript […], take the current text contents of the clipboard and paste into an existing file, overwriting any existing contents of the file
AppleScript code:
do shell script "pbpaste > /path/to/your/clipboard-file.txt"
In order to read text from the file back into the clipboard, use
do shell script "cat /path/to/your/clipboard-file.txt | pbcopy"
For documentation, see man pbpaste
EDIT: Now, to convert the AppleScript into a Mac OS X Service, which will appear in the "Services" group of every context menu (at right-click / CTRL+click on any text), you can use Automator, as described in this tutorial.

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.

run applescript on 2x-click

OK, this feels like an idiot question, but I'm stuck - I don't know the first thing about AppleScript. I have a .scpt file and I want to double-click it and just have it run, but instead every time I click, it opens up the AppleScript Editor. This feels like it should just be an option on the file, but I'm missing something obvious.
Please help me feel less dumb, thank you.
From the “File” menu, choose “Export”; there’ll be a “File Format” dropdown underneath the file browser. To get a double-clickable application instead of a document, choose “Application”. This will produce a .app bundle like ordinary Mac applications (this will also let you package other resources with your script if you need to). You can choose “Run Only” or not; if you do, then anybody with just the .app won’t be able to edit your script further, since it’ll be compiled. (But if you’re saving a copy as the application, that might be what you want.)
Another option, as per an anonymous user on Ask Different, would be to save/export your file as a “Script” (.scpt) or “Script Bundle” (.scptd), save it in ~/Library/Scripts/, and check “Show Script menu in menu bar” in Script Editor’s preferences.
(If you’re running an old version of OS X, the first version of this answer has the information you’re looking for.)
There's more than one way to do it; i have found this to be the simplest:
In sum, you create an Automator application and place your applescript inside it (easier than it sounds, and it's not a hack either--there's actually a specific Automator action for this). Then when you are finished, you select "File" from the menubar, next "Save As Application", then select a location. Now check there and you'll see the newly-created Automator icon (little white robot holding a grenade launcher).
You can do anything that you would ordinarily do with this application icon--double click to open, drag it to your dock, etc.
Appstorm has created an excellent step-by-step tutorial for building an applescript-embedded automator action. On the page i linked to, the tutorial author has also supplied an Automator script that you can download and use as a template.
While it's certainly not the simplest route, one benefit to running your script from Automator, as doug suggested, is that you can set a hotkey or keyboard shortcut to execute your script if you hide it in an Automator Service (OSX 10.6+). See:
http://blog.fosketts.net/2010/08/09/assign-keyboard-shortcut-applescript-automator-service/
When you save a new script, a menu should appear asking what you want the file name to be, where it will be stored, any tags for it, and what script format you want it to be. There should be 4 scripts formats:
Script
Script Bundle
Application
Text
The script format you want to use would be "Application." This will turn it into a double-click application if its not in the dock.

Resources