AppleScript to paste text from clipboard into a file - macos

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.

Related

Batch copy file name action in Apple Script for Automator Mac OS X

Can one automate the process of copying a file name and pasting it in the Spotlight Comment box of that file? How does that Apple Script for OS X looks like?
I would need to do that a thousand times otherwise:
Hitting enter to select the file
Short cut typing Hotkey Cmd C
Short cut typing Hotkey Cmd I to open that info window
Hitting tab button twice to enter the Spotlight Comment box
Short cut typing Hotkey Cmd v
Short cut typing Hotkey Cmd w to close that info window
The aformentioned folder contains a thousand files and even more.
Automator has that function built right in, via the Set Spotlight Comments for Finder Items Action:
You should be able to point the first action at your target folder, have it find all the files, and it will add the Comment to all of them.
Good luck,

"Open folder in Terminal" for Spotlight or Alfred

Is there any way in Spotlight to have a folder open in a Terminal window, either by providing an "Open in Terminal" option in search results, or (ideally) by holding down a key when selecting a result? Basically the same way holding down command will "Open containing folder" maybe make it so holding option (or whatever key) could "Open in Terminal".
Alternately, is there a way to setup the Alfred app so that it will always open folder search results in the Terminal? that might be even better, then I could use ctrl-space exclusively for opening folders in the Terminal and command-space (Spotlight) for everything else.
The closest thing I can find to anything like these is the "cdto" application, which adds an "Open in Terminal" button to the Finder. This is nice, but being able to do this directly from Spotlight or with a keypress would be much more convenient.
This question is old and you undoubtedly found the answer by now; for the sake of completeness: the latest version of Alfred allows one to search/navigate the filesystem from within Alfred, and take action on the folder or file found. One folder action is "Open Terminal Here", which seems to be what you are asking for.
I've found this to be the fastest method without any external tinkering.
Requirement: need to have iTerm or Terminal in the Dock:
Type the folder name into Alfred / Spotlight
Click and drag the folder that was found into the iTerm / Terminal icon in the Dock
Try this tool:
https://github.com/nmadhok/OpenInTerminal
This is a really handy tool for programmers on Mac as it lets you open the folder directly in Terminal. You can select multiple folders to open them in multiple terminal windows. You can also select files to open the parent directory in Terminal. This application works with Finder as well as without Finder which is a plus!
use this Alfred extension
https://github.com/LeEnno/alfred-terminalfinder
then just lob off the input and set it to a hotkey.. I have one to open the current finder in terminal and vice-versa
#Verboze - I'm still looking for an answer :)
Perhaps it should be migrated to stack exchange or the other one used non-programming topics, such as system administrivia. Though a case could be made that the command line is essential to programming.....
Windows has "Command Prompt Here". Linux has Nautilus' "Open In Terminal". The world's most advanced operating system has nothing (the best I can tell).
For completeness, I did look at the customizable toolbar ('drag your favorites here'; but my favorite is missing). I also found an old Action Script or Automator Script that did not work (the article was old, and it could not be followed).
I did find a program called "CD To...", but its broken. Upon opening the first terminal, the program opens two terminals. I'm not sure what other programming mistakes were made by the developer, and I really don't want to experiment.
Check this: https://github.com/miromannino/alfred-new-terminal-window
It opens a new Terminal/iTerm window in the current space.
Holding the alt key, the new window is also opened opened in the current frontmost Finder folder.
For me that is almost sufficient for my daily work!
Hope it helps!

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.

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.

Open dot-file with dialogue in OSX

Since the GUI-side of OSX treats all dot-files (such as .htaccess) as hidden, it doesn't display them in any of the graphical UI:s, e.g. Finder or the Open-dialogues.
How can I open a dot-file (.htaccess in this case) in a graphical editor, without doing that thing for all hidden files, universally and without going through Terminal.app?
Edit: I'm on Leopard, if that makes a difference.
Edit2: TextWrangler and TextMate seem to have features that allow you to open hidden files, which partly answers my question.
In an "Open File" dialog you can use Command-Shift-. to see dot files.
You could tell Finder to display hidden files as well (enter in Terminal):
defaults write com.apple.finder AppleShowAllFiles TRUE
But that’s not really nice since there are a lot more hidden files. So I recommend to use an editor that allows you to view those in the open dialog like Chuck mentioned.
If you only want to do this for one specific file, you can create a symbolic link to the dot-file. Open up Terminal.app, cd to the directory containing your dot-file, and run
ln -s .htaccess dot_htaccess
Then you should be able to double-click the file dot_htaccess as a regular file, and any edits you make will really go into .htaccess.
TextMate (a really nice text editor for OS X) open dialog has a "Show hidden files" option, and TextWrangler (and its big brother BBEdit) has it has a menu item.
In the command line, for a file named FILE, type:
open -e FILE
The open command will open the file in TextEdit (-e flag). Check out "man open" for more flags (e.g., specify the app to open with -a)
Smultron (another nice OS X editor mentioned in the comment above and similar to TextMate, but free) has an "Open Hidden..." file menu item that works splendidly for this purpose.
Sad news: Smultron is apparently no longer being developed further beyond v3.5.1 (which requires Leopard), according to a post from its author at its homepage: http://tuppis.com/smultron/
jEdit is another free option that has hidden file support: www.jedit.org/users-guide/vfs-browser.html (sorry for the lack of 'http' in the link -- being a new user, my posts are limited to just one link. Alas...)

Resources