I'm a novice at coding so please be patient with me.
I've created a Workflow with Automator (OSX) which works fine. The only issue I have is that I want it to run on a number of inputs (that is as a batch). I've inserted the Loop action but the problem I'm having is about changing the initial input each time.
I would like to use an applescript to automate the insertion of the initial input each time.
I have a TXT file with URLs. With an apple script, I'd like to copy a URL (or a line of text) to clipboard.
In the next iteration I'd like to copy the next URL (or line of text).
Can anyone help?
Thank you!!
You can create one looping workflow (called as LinesToClipboard.workflow) what will
get a line from an text file (not rtf, or doc)
copy the line to clipboard
run your current workflow
loop again for the next line
The workflow:
Create new automator workflow
create a variable
at the bottom find the icon "Show or hide the workflow variables list" and show the workflow wariables (empty)
right click and "New variable..."
name the variable as "LineNumber"
add actions:
Get Value of Variable (LineNumber)
Run Shell Script
shell: /bin/bash
important: change the Pass input to as arguments
add the following content (copy exactly, with all quotes and such):
in the content of script, change the /etc/passwd to the full path of your filename, like /Users/myname/Documents/myfile.txt
at the end of this action the clipboard will contain one line from the file
linenum=${1:-0}
filename="/etc/passwd" # full path of your text-filename
let linenum++
sed -n "${linenum}p" < "$filename" | pbcopy
echo $linenum
Set Value of Variable (LineNumber)
Run Workflow - add your current workflow (or the "ShowClipboard.workflow" - see bellow)
the Wait for workflow to finish should be checked
important The Output menu should be: "Return action input"
Loop
add your count...
Run Shell Script (Ignore this action's input), content one line: echo 0 (This will reset the variable LineNumber to zero, when the loop ends)
Set Value of Variable (LineNumber)
For testing, you can create another workflow, called ShowClipboard.workflow, with an content:
Get Contents of Cliboard
Set Value of Variable (clipval)
Ask for confirmation (and drag the (clipval) to the Message field)
Run the first workflow.
Screenshots (for sure) :)
The second workflow (for testing)
You don't need AppleScript to get the URLs but can directly extract them with Automator by using a shell task. After using the task that's getting contents of a folder (this is a Finder task in Automator), add a shell task as the next task. Make sure you select that the input is send as arguments instead of sending it to stdin. When you have done that you only need something like one of the following shell scripts.
cat $# | egrep -io '\S?(http|https|ftp|afp|smb|mailto|webcal):\S+''
It first read all the files using cat. The $# is a shell variable that contains the arguments collected by the previous task: A list of paths to all files of batch folder. We pipe them to egrep will which will only output the URL filtered by their schemes. If you want to support any scheme (official and unofficial schemes):
cat $# | egrep -io '\S?[A-Z][A-Z0-9+-.]+:\S+'
Related
I'm writing a shell script. what it does is it will create a file by the input that is received from the user. Now, i want to add the feature called "view a file" for my current script. Now, it's unreasonal to retype it again since i've already had a script that helps
I know it's crazy when it is possible to it with normal shell command. I'm actually writing a script that help me to create pages that are generated from the touch command. (this pages had attached date, author name, subjects, and title).
The question is how to call a another script or inhere another script?
Couple of ways to do this. My prefered way is by using source
You can -
Call your other script with the source command (alias is .) like this: source /path/to/script.
Make the other script executable, add the #!/bin/bash line at the top, and the path where the file is to the $PATH environment variable. Then you can call it as a normal command.
Use the bash command to execute it: /bin/bash /path/to/script
Some times that I have a command ready to press enter but that command I have changed it in some way and it's a long command, then I remember that I have to open a text file (e.g. to get some information that I will use in the command). So what most of the times I do, is to cancel that command (Ctrl+C) and then open the text file get the information I need and then retype the command again with the pasted value from the text file. This is not very efficient for me specially if the server doesn't have any kind of GUI and I can't copy the previous command so I don't lose it.
So my question is, Is there any kind of combination keys that I could use to save a command ready to enter so I don't lose it and I don't have to type it all over again?
Thanks!
This is currently not possible out of the box.
The easiest way to do it is probably to
Change the cancel binding to stash the commandline
Add a binding to recall the stashed commandline
It would work something like this:
The function to recall:
function recall_commandline
if set -q stashed_commandline
commandline -r -- $stashed_commandline
end
end
Add to __fish_cancel_commandline (use funced __fish_cancel_commandline. Once you are happy do funcsave __fish_cancel_commandline):
set -g stashed_commandline $cmd
# right before:
commandline ""
Add to fish_user_key_bindings
bind \cr recall_commandline
This will allow you to press Ctrl+r to recall the last cancelled commandline. Expanding it to multiple is non-trivial (since "commandlines" can have multiple lines), as is adding the commandlines to history so that they can be recalled with the normal bindings.
I have the following function to turn comment/uncomment the current statement:
function toggle-comment-cmd-buffer --description 'Comment/Uncomment the current or every line'
set -l cmdlines (commandline -b)
if test "$cmdlines" = ""
return
end
set -l cmdlines (printf '%s\n' '#'$cmdlines | string replace -r '^##' '')
commandline -r $cmdlines
string match -q '#*' $cmdlines[1]; and commandline -f execute
end
I bind it thusly: bind \e\# toggle-comment-cmd-buffer. This way I can quickly comment and put the current statement in my command history in order to do something else. Then I can recall the comment and press [alt-#] again to remove the comment characters and continue modifying the command.
I set this up in my personal fish config because I had gotten used to doing something similar in ksh93.
I'm a very novice and infrequent applescript experimenter. I've tried for several hours now to learn the individual applescript commands for the following task, but I always run into errors. Perhaps someone much more adept at applescript will find this task easy and quick, and for that I would be very grateful. Here is the task:
I want to be able to manually select a document or file within the finder and then execute the following unix command on that file. I would then store the script under Finder's "Services" menu. The unix command is:
srm -rfv -m path/filename
In my attempts, I assumed that a script that would open Terminal and execute the command would be the way to go, but just couldn't get anything to work. Thank you in advance to any good programmers who can whip out such a script for me.
My tip: Create such services using Automator!
Create a new Service in Automator
Choose "File & Folder" as Input and "Finder"
Add "Run shell script"
Choose "as arguments" as input
Change echo "$f" to your command srm -rfv -m "$f"
Save it as "Safe delete"
From now on, if you select a file inside Finder you will find the option "Safe delete" in the context menu.
Enjoy, Michael / Hamburg
Craig's comment is pertinent, but I am just focus on the script itself, not on the shell command. the script bellow must be saved as Application and each time you drop 1 or more file on its icon, the shell script command will be executed for each file :
on open myFiles
repeat with aFile in myFiles -- repeat loop in case you drop more than 1 file on the icon script
try
do shell script "srm -rfv -m " & (quoted form of POSIX path of aFile)
end try
end repeat
end open
Still make sure that in your shell command 'srm -rfv', the 'v' is necessary because this script will not display any thing ! I don't think so. also I did not display error during remove. what do you want to do with error (like write protect, ...) ?
Update: I missed that the OP wants to create an OS X Service that integrates with Finder. ShooTerKo's answer shows how to do that (and his solution doesn't even require use of AppleScript).
The only potentially interesting thing about this answer is that it demonstrates AppleScript commands to open a file-selection dialog, get the chosen file's POSIX path and run a shell command with it, with some general background information about executing shell commands with do shell script.
Try the following:
# Let the user choose a file via an interactive dialog.
set fileChosen to choose file
# Get the file's POSIX path.
set filePath to POSIX path of fileChosen
# Use the path to synthesize a shell command and execute it.
do shell script "echo srm -rfv -m " & quoted form of filePath
Note:
There's no explicit error handling; if you don't want the script to just fail, you'll have to add error handling (try ... on error ... end try) to handle the case of the user canceling the file selection and the shell command failing (unlikely in this case).
The shell command has echo prepended to it in order to perform a dry run (see its output in Script Editor's Result pane); remove it to perform the actual deletion.
quoted form of is important for ensuring that a string value is included as-is in a shell command (no risk of expansion (interpretation) by the shell).
do shell script will NOT open a Terminal window - it will simply run the shell command hidden and return its stdout output, which is usually what you want. If the shell command signals failure via a non-zero exit code, an error is raised.
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 $_
I have a weird looking problem (a similar one was asked here, but I don't want to accept that Automator only pipes only 1 line into the shell script action!: Mac Automator: shell script gets only one line)
Automator-Workflow, type "service", 3 blocks:
Service receives "Text"
"run shell script", "bash", input via "stdin", shell script: "cat"
copy to clipboard"
When I select a multiline text and run this service only the first line finishes in the clipboard.
I made three other tests:
skip the shell script action - directly move the selection into the clipboard >> works!
instead of taking the input from the text selection the shell script action gets the input via an "read from clipboard" action from the clipboard >> fails (first line only)
instead of the "bash" action I selected a "perl" action >> fails (first line only)
So it seems obvious that the run shell script action contains the problem.
But I have used the shell script action (with web-content) many times before with no problems.
Any ideas?
Maybe a problem with encoding and or line-endings?
At least on my Mac, when
start Automator
Choose Type -> Service
save at some name (mine is TestService), then
go to TextEdit
enter some text
select
from the TextEdit's menu: TextEdit -> Services -> TestService
Got to the clipboard the next:
2 ééééééééééé
3 íííííííííí
4 αβγδεζη
5 ЧШЩЪЫЬЭ
6 aaaaaaaaaa
Try exactly the above... ;)
I ran into the same issue. Instead of using the 'copy to clipboard' command I then tried executing another shell script (setting its input to 'stdin') containing only the command 'pbcopy'.
Afterwards line breaks were preserved properly.