Jump to content
Bhishan

[SOLVED] Problem with copying screenshot to current directory

Recommended Posts

I made a simple workflow to capture screencaputure, rename it, and move it to the current directory of the Finder.

 

The workflow works for folders like "Downloads", "Dropbox/hello", but it fails when I want to write to "Google Drive".

 

There is escaping problem.

 

I tried changing $1 to "$1" in the bash script, but it did not worked.

 

How the problem can be fixed?

 

The workflow is shared here:

https://github.com/bhishanpdl/Shared/blob/master/Alfred_questions/aa screenshot copy to current directory.alfredworkflow?raw=true

Edited by Bhishan

Share this post


Link to post

Having had a brief look at the workflow and the bash scripts, my guess is that when you substitute in "Google Drive", you're not using quotes to enclose the full file path, e.g. where you have, in one script:

/bin/mv $a ${HOME}/Dropbox/KeepMe/KeepScreenshot/${name}.png

would be better if it were:

/bin/mv "$a" "${HOME}/Dropbox/KeepMe/KeepScreenshot/${name}.png"

 

My other suggestion, though unrelated to your present dilemma, is that you combine your four Run Script actions into a single action.  It doesn't make much sense to have them separate, and it'll make debugging easier too.

Share this post


Link to post

In fact, this is your combined script just as a straight copy-n-paste (it obviously won't function yet because of the naked AppleScript code):

screencapture -i -x ${HOME}/Dropbox/KeepMe/KeepScreenshot/$(date +%Y-%m-%d-%H-%M-%S).png

# rename

sleep 5
a=${HOME}/Dropbox/KeepMe/KeepScreenshot/*.png

/bin/mv $a ${HOME}/Dropbox/KeepMe/KeepScreenshot/${name}.png

tell application "Finder"
    if exists Finder window 1 then
        set currentDir to target of Finder window 1 as alias
    else
        set currentDir to desktop as alias
    end if
end tell
return POSIX path of currentDir

# cd to cwd
cd $1

# move png to cwd
/bin/mv ${HOME}/Dropbox/KeepMe/KeepScreenshot/${name}.png ${name}.png

# copy final path to clipboard
pbcopy < ${name}.png

There's a lot of renaming/moving and so forth going on, and I'm wondering whether it would have been easier just to set the destination path straight away when taking the screen capture.  That way, the entire script above gets reduced to this:

fp=$(osascript -e \
	"tell app \"Finder\" to get insertion location as alias
	 return the result's POSIX path")$name.png

screencapture -i -x "$fp"

# copy final path to clipboard
printf '%s' "$fp" | pbcopy

 

Share this post


Link to post

@CJK  Thanks for your help, but I got one small problem.

 

 I tried to get only the basename of the png file instead of the whole POSIX path, but it failed.

 

My attempt:

I added following at the bottom of the script.

 

# copy basename of png to clipboard
base=`basename "$name"`
echo -n "$base" | pbcopy

 

How can the workflow only give the base name of the png?

Share this post


Link to post
23 hours ago, Bhishan said:

How can the workflow only give the base name of the png?

 

Sorry, I see now that your original script returns the basename.  I mistook it originally for returning the full path, so engineered my version of the script to do the same.  It's one small modification in the way I declared the variable fp, and concatenated it with $name from the outset.  Instead, we'll just keep them as two separate variables, so $fp will contain the file path to the containing folder, and $name will contain the basename that you're after:

fp=$(osascript -e \
	"tell app \"Finder\" to get insertion location as alias
	 return the result's POSIX path")

screencapture -i -x "$fp$name.png"

# copy final path to clipboard
printf '%s' "$name.png" | pbcopy

 

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×