Can AppleScript take output of shell script and put in paste buffer? - shell

Can AppleScript take the output of a shell script or variable and put it in the paste buffer?
I have a file with a password for every day (formatted "date,password") and I want to write a script that when run will look up the date and output the password for that date.
That part is not a problem, I'm just wondering if there is a way to get the output to automatically go into the paste buffer?

Using AppleScript to put output of a shell command into the clipboard:
set the clipboard to (do shell script "ls")
If you do not want to use AppleScript you can use pbcopy in the shell:
sh$ some command | pbcopy

Related

Is there a way to simulate Save and Close a file through shell command?

I have a scenario where a shell script runs another shell script. The second shell script prompts user to type review comments and close the file.
In my case, I do not want to type anything but only save and quit (equivalent vim command :wq) the file. I want to automate this through a shell script where I don't want manual intervention to save and quit the file. How can i achieve this?
I'm not sure i get the context but if you simply (echo "input of your choice" >> file.txt ) it will do the job and will not have to quit and save anything
if th einput is already in a file you can (cat inputfile.txt >> file.txt )
this command can be place inside of your first script

Copy a string to clipboard from Mac OS command line

is there a way to copy a string to clipboard from command line?
To be more specific, I want to make a script which copies my email address to clipboard, so that when I need to insert it several times for logging in / register, I just run the script once and then CMD+V it whenever I need.
I heard of pbcopy, but I think this is not my case. Any suggestion?
Many thanks!
You need to pipe the output of your script to pbcopy
For example:
./somescript.sh | pbcopy
echo 'your-email#example.com' | pbcopy
(as #Jonathan Leffler stated above)
Or see the answer for the related question on piping to clipboard on different operating systems:
Pipe to/from the clipboard in Bash script

How do I exit pbcopy in the Mac Terminal?

If I just enter pbcopy by itself in Terminal, it appears to prompt for user input. I'm assuming that the idea is you enter some input, then exit or end pbcopy, and then your input is added to the clipboard, so you can pbpaste it or whatever.
I can't figure out how to end pbcopy, without using Control-C which kills the process and doesn't save my data to the clipboard.
Still very new to Terminal. Thanks!
For most terminal programs, end of input is triggered by Ctrl-D. This is considered the valid conclusion of input from the command.
It could be something else, but the key combination can be read from the output of stty -a, which shows an entry: eof = ^D, which indicates Ctrl-D is that key combination.
Now programatically, if you're trying to get input into pbcopy, you can do, from a file:
cat file | pbcopy
or the slightly more 'shell purist' (doesn't waste a process with the cat):
pbcopy <file
from a bash script, using a here document:
pbcopy <<EOM
copy copy copy...
EOM

Bash shells script 101 - variable definition, pbcopy, etc

I am trying to create a (my first) bash script, but I need a little help. I have the following:
#!/bin/bash
echo "Write a LaTeX equation:"
read -e TeXFormula
URIEncoded = node -p "encodeURIComponent('$(sed "s/'/\\\'/g" <<<"$TeXFormula")')"
curl http://latex.codecogs.com/gif.latex?$URIEncoded -o /Users/casparjespersen/Desktop/notetex.gif | pbcopy
I want it to:
Require user input (LaTeX equation)
URIEncode user input (the top result in Google was using node.js, but anything will do..)
Perform a cURL call to this website converting equation to a GIF image
Copy the image to the placeholder, so I can paste it to a note taking app like OneNote, Word, etc.
My script is malfunctioning in the following:
URIEncoded is undefined, so there is something wrong with my variable definition.
When I copy using pbcopy the encrypted text content of the image is copied, and not the actual image. Is there a workaround for this? Otherwise, the script could automatically open the image and I could manually Cmd + C the content.
URIEncoded is undefined, so there is something wrong with my variable
definition.
The line should read
URIEncoded=$(node -p "encodeURIComponent('$(sed "s/'/\\\'/g" <<<"$TeXFormula")')")
without spaces around the = sign, and using the $() construct to actually perform the command, otherwise, the text of the command would be assigned to the variable.
When I copy using pbcopy the encrypted text content of the
image is copied, and not the actual image. Is there a workaround for
this? Otherwise, the script could automatically open the image and I
could manually Cmd + C the content.
pbcopy takes input from stdin but you are telling curl to write the output to a file rather than stdout. Try simply
curl http://latex.codecogs.com/gif.latex?$URIEncoded | pbcopy
or, for the second option you describe
curl http://latex.codecogs.com/gif.latex?$URIEncoded -o /Users/casparjespersen/Desktop/notetex.gif && open $_

How to execute command inside vim?

I want to run shell script inside Vim editor.
I heard it is possible but do not know.
Command:./shell.sh inside vim.
There are multiple ways to do it. A primary question is "Do you want the output from the script in the file?"
If you want the output in the file:
:r!./shell.sh
If you don't want the output in the file:
:!./shell.sh
If you have the line ./shell.sh in the file, you can include the output in the file with:
!!sh
If you've done it before, you have more options.
If you save the command in a named buffer you have still more options.
If you want the script to have a portion of the file (edit buffer) as its standard input, you have an enormous number of options you can use in conjunction with either of these mechanisms.
Prefix the command with a !. For example, open Vim and write:
:!ls
This will execute the shell ls command.
Note that you'll have to be in the correct directory within Vim for this to work.

Resources