I would like to write a python function to obtain the gif file given the URL, and store it in Mac's clipboard. Could anyone please help me figure out how to copy GIF into clipboard?
(Let's say the URL is very simple and direct to the GIF I want, "a.com/this.gif".)
You can use the following applescript to copy a gif to the clipboard:
osascript -e 'set the clipboard to (read (POSIX file "/Users/auser/yourgif.gif") as GIF picture)'
You can point the clipboard directly at the file instead of reading its contents:
osascript -e "set the clipboard to (POSIX file \"$f\")"
You can combine this in a script to automatically do the work:
#!/usr/bin/env bash
set -euo pipefail
f="$(mktemp).gif"
curl -o "$f" --fail "${1?Usage: $0 <url-of-gif-file>}"
osascript -e "set the clipboard to (POSIX file \"$f\")"
# Don't delete the tempfile: it holds the GIF. It will be cleaned up on reboot.
Inspired by https://stackoverflow.com/a/51447031/4359699
This would work for any file type by the way, not just GIF.
In Mac OS X, you can use the pbcopy command to copy content to the system clipboard. To achieve this from your Python script, I imagine you would want to fetch the resource (I imagine using urlopen), and then use the subprocess module to invoke pbcopy with the content of the GIF file that you fetched.
Related
I want to run a command in bash say for instance ./command -i INPUT_FILE -o OUTPUT_FILE which takes two parameters, 1) a path to an input file and 2) a path to an output file. It processes the INPUT_FILE and writes the results in the OUTPUT_FILE. Is there any way that I can provide the INPUT_FILE and OUTPUT_FILE as some variables? So that instead of stored files in the disk, I want to feed/store them as variables in the memory. Note that the output is written in the provided file path, not stdout (otherwise, it was obvious). In the core of the command it opens ofstream in C language to write the results in the OUTPUT_FILE.
I searched and reached a solution for the input part which is working but not for the output part. Here is the suggested solution:
./command -i <(cat <<< "$INPUT_VARIABLE") -o OUTPUT_FILE
Is there any suggestion for the output part? My end goal is using this ability in Python but it seems subprocess module doesn't have this feature.
You can use tmpfs for that, which is a virtual filesystem, located in RAM.
For your current user you can use /run/user/$UID/ directory, so something like
./command -i <(cat <<< "$INPUT_VARIABLE") -o /run/user/$UID/OUTPUT_FILE
can totally work. Just rm that file if you don't need it after your script has stopped. This file won't survive a reboot, obviously.
It's about how to get a list of URLs by using youtube_dl. Although I have been trying all day long, I couldn't work it out. Thus I would like to ask help to translate the following command lines (partially in Linux) into Python codes. I mean in a .py file.
To get JSON data, use command line: youtube-dl -j --flat-playlist 'https://www.youtube.com/c/3blue1brown/videos'
To parser use command line in Linux: youtube-dl -j --flat-playlist 'https://www.youtube.com/c/3blue1brown/videos' | jq -r '.id' | sed 's_^_https://youtube.com/v/_'
The codes above are from: https://web.archive.org/web/20180309061900/https://archive.zhimingwang.org/blog/2014-11-05-list-youtube-playlist-with-youtube-dl.html (The youtube link there was removed so I replaced the youtube link above)
You can use the same command to run inside a .py file using os as follows:
import os
os.system("youtube-dl -j --flat-playlist 'https://www.youtube.com/c/3blue1brown/videos'")
You can pipe the output of the above commands to a file and then process your json file in python.
Python: 2.7
Mac OSX: 10.12
I am brand new to using apple script and I am trying to use it to basically open a Python file (from its current directory without having to define the full path) that uses tkinter to open a GUI. Also, I do not want the terminal application to open.
I have both scripts below that I have found on stackover flow:
1.) Opens the Python file with AppleScript from current directory (without defining the full path in the script):
tell application "Finder"
open file "pythonfile.py" of folder of (file (path to me))
end tell
2.) Opens a Python file with AppleScript, and does not open the terminal application when ran.
do shell script "export LC_ALL=en_US.UTF-8; export LANG=en_US.UTF-8; /usr/bin/python '/full/path/to/Pythonfile/pythonfile.py' &> /dev/null &"
So I basically need help combining the two answers above, so that I do not have to write the full path in #2 and the terminal application window does not open when ran, which it will open in #1.
get the path in Unix style
set scriptPath to (POSIX path of ((path to me as text) & "::") & "pythonfile.py") as text
then launch it
do shell script "export LC_ALL=en_US.UTF-8; export LANG=en_US.UTF-8; /usr/bin/python " & quoted form of scriptPath & " &> /dev/null &"
Basically I have a bash script that wget's the html of a page, convert it to xhtml using tagsoup and then extracts elements via the python script. Nothing seems to work, I just need the output of that python script to be stored in a new $d csv file.
#!/bin/bash
while sleep 10s
do
d=`date '+%Y-%m-%d-%H-%M-%S'`;
wget -O $d.html http://wsj.com/mdc/public/page/2_3021-activnyse-actives.html
java -jar tagsoup-1.2.1.jar --files $d.html
python3 idk.py $d.xhtml
done
~
From what I understand, you have just to use that command line :
python3 idk.py > $d.xhtml
Is it possible for us to copy contents of a .tar.gz file using echo command?
I am using telnet(through telnetlib in python) to execute commands in a server. I need to copy few files into the server. However, scp just hangs after authentication. The server is a busybox server. Another team is looking into the issue for now. The scp command I used is this:
scp -i /key/private.pem /home/tempuser/file.tar.gz tempuser#remote1:/tmp/
I side stepped by reading the contents of the file, put them in the echo command in the remote. However, when I try to read a tar.gz file, it fails. I could not untar the file and copy the files within it as the tar file has nearly 500 files in it. Including a few tar files.
So any possible way to copy a tar file contents(read through open command in python) without scp?
Or is it possible to copy a file using the telnetlib in python? using the Telnet function?
To be more clear, I need to upload a tar.gz file from local machine to the remote machine. But without the help of scp. It will be more helpful if it is a python solution. If bash is the way to go, I could run os.system too. So python/shell scripting solution is what I am looking for.
If you need any more information, please ask away in the comments.
You can cat and redirect, for example:
ssh user#server cat file.tar.gz > file.tar.gz
Note that cat will happen at the server side, but the redirection will happen locally, to a local file.
You could also directly gunzip + untar to the local filesystem:
ssh user#server cat file.tar.gz | tar zxv
To do it the other way around, copy from local to server:
ssh user#server 'cat > file.tar.gz' < file.tar.gz
And gzip + tar to the server:
tar zc . | ssh user#server 'cat > file.tar.gz'
if you try to the run the command outside of the python script it will ask you for password:
scp -i /key/private.pem /home/tempuser/file.tar.gz tempuser#remote1:/tmp/
to pass the password for Unix scp/ssh command you need to redirect the password as input to the command like:
myPass > scp -i /key/private.pem /home/tempuser/file.tar.gz tempuser#remote1:/tmp/
There is an alternative method using the base64 utility. By base64-encoding the file you wish to transfer, you'll avoid issues with any escape chars, etc. that may trip echo. For example:
some_var="$( base64 -w 0 path_to_file )"
ssh user#server "echo $some_var | base64 -d > path_to_remote_file"
Option -w 0 is important to prevent base64 from inserting line breaks (after 76 characters by default).