Jump to content
MarcG

[SOLVED] Screenshots to preview. Is this possible?

Recommended Posts

I've just found/bought Alfred mainly to help me save time entering text I have to repeat many times a day but looking deeper into the site it seems there is a world of possibilities out there.

 

One thing I do a lot is take a screenshot (using cmd+ctrl+shift+4) to the clipboard, open preview, go to the file menu and then create a new image from the clipboard. It's not a hard process, but it's done enough to be annoying.

 

Is it possible to automate that, so I can initiate a screenshot and as soon as it's done it opens it in Preview?

 

And if it is, could you help, or at least point me down the road on how to achieve it.

 

Many thanks!

Share this post


Link to post

Hi @MarcG, welcome to the forum.

 

Alfred isn't an automation tool in the vein of, say, Automator or Keyboard Maestro: it's a launcher. As such it doesn't give you many automation-style tools, and the Alfred forum isn't the first place you should ask automation questions.

 

AFAIK, what you're asking is made trickier by the fact you want to wait for the screenshot. You can't wait for actions triggered by simulated keypresses, so that means you'll probably have to use the screencapture command instead.

 

So you'll probably want to:

 

  1. Run screencapture
  2. Activate Preview
  3. Simulate ⌘N to create new image from Pasteboard

Share this post


Link to post

this is working for me, using a run NSApplescript action in a workflow:

 

on alfred_script(q)
-- path to the desktop
    set theDesktop to POSIX path of (path to desktop as string)

-- variables to form the name of the snapshot
    set {year:y, month:m, day:d} to current date
    set myDate to y as string & "-" & m as string & "-" & d as string
    set myTime to time string of (current date)

-- command to take the snapshot
    set shellCommand to "/usr/sbin/screencapture " & theDesktop & "Screenshoot-" & myDate & "-" & myTime & ".png"
    do shell script shellCommand

-- command to open the snapshot
    set shellCommand to "open " & theDesktop & "Screenshoot-" & myDate & "-" & myTime & ".png"
    do shell script shellCommand

end alfred_script

 

obviously you should use an alfred shortcut to start the workflow, instead of cmd+ctrl+shift+4

Edited by juliosecco

Share this post


Link to post

screencapture, which comes with macOS, is surprisingly capable (man screencapture) and can do it in a single command:

screencapture -iP "$(mktemp).png"
  • i is to capture interactively (i.e., the same as ⌘⇧4).
  • P is to open the image in Preview after it’s done.
  • "$(mktemp).png" is a temporary file to store the screenshot, which screencapture needs.

 

Add that in a Run Script Action connect by a Hotkey Trigger, and you’re set. Here’s a ready-made Workflow.

Edited by vitor

Share this post


Link to post
32 minutes ago, vitor said:

screencapture, which comes with macOS, is surprisingly capable (man screencapture) and can do it in a single command:


screencapture -iP "$(mktemp).png"
  • i is to capture interactively (i.e., the same as ⌘⇧4).
  • P is to open the image in Preview after it’s done.
  • "$(mktemp).png" is a temporary file to store the screenshot, which screencapture needs.

 

Add that in a Run Script Action connect by a Hotkey Trigger, and you’re set. Here’s a ready-made Workflow.

 

It's painful being a newbie, sorry...

 

So, all I need to do is import that workflow, set the hotkey and it should work? 

 

I tried to do that (cmd+ctrl+shift+1) and nothing happens when I hit the hotkey sequence. So I need to activate it in some way or give some permission in Alfred like I did to make text replacement work?

Share this post


Link to post
1 minute ago, MarcG said:

So, all I need to do is import that workflow, set the hotkey and it should work? 

 

Yes.

 

1 minute ago, MarcG said:

I tried to do that (cmd+ctrl+shift+1) and nothing happens when I hit the hotkey sequence.

 

Look at your cursor. It gives no warning, but it should have changed to the cross that allows you to select an area for the screenshot.

Share this post


Link to post
Just now, vitor said:

Look at your cursor. It gives no warning, but it should have changed to the cross that allows you to select an area for the screenshot.

 

 

Okay, not that much of a newbie! ?

 

The cursor doesn't change, I was expecting it to look like it does when I do a cmd+ctrl+shift+4, but it stays an arrow.

Share this post


Link to post

  1. Open the debugger, set the log output to “All Information”, and post the output.
  2. Run the command in a Terminal. What happens?
  3. What is your versions of Alfred?
  4. What is your versions of macOS?

Share this post


Link to post

Okay, the issue is pretty obvious...

 

Quote

[2018-05-18 11:28:21][ERROR: action.script] /Users/marcgoldman/Library/Caches/com.runningwithcrayons.Alfred-3/Workflow Scripts/81226899-3A6C-4671-A0C7-7147A39F74CC: line 1: screencapture: command not found

 

I've tried updating it to /usr/sbin/screencapture but that didn't help.

 

If I run it in terminal, I get a command not found, but if I just run screencapture with no parameters, it does find it, as the error is then that no filename is specified.

 

Quote

MacBook-Pro-3:~ marcgoldman$ screencapture -iP "$(mktemp).png"

-bash: screencapture: command not found

MacBook-Pro-3:~ marcgoldman$ screencapture

screencapture: no file specified

MacBook-Pro-3:~ marcgoldman$ 

 

Latest Alfred and OSX 10.13.4

Share this post


Link to post
1 hour ago, MarcG said:

Latest Alfred

 

Tip when debugging: never say “latest” but always the exact version. When you say “latest” we don’t know if you mean the latest beta or stable version, nor do we know if you’re actually on the latest version. You may think you’re on the latest version but me mistaken, or might be a version behind because the auto-update hasn’t kicked yet, or I may not be on what I think is the latest version but isn’t, or a multitude of other reasons. It also does not help someone who comes later with the same issue, as they’ll have no idea if their version matches yours.


That said, no need to fix it this time, as the issue is not with Alfred.


I’m on 10.12, so it’s possible screencapture changed some option between versions.


In a Terminal, do pbcopy < <(man screencapture | col -b). Then paste the result somewhere like pastebin, so we can see the manual for your version.

 

@juliosecco Are you also on macOS 10.13.4?

Share this post


Link to post
3 minutes ago, vitor said:


In a Terminal, do pbcopy < <(man screencapture | col -b). Then paste the result somewhere like pastebin, so we can see the manual for your version.

 

@juliosecco Are you also on macOS 10.13.4?

 

No, I'm on 10.12.6,

 

and your workflow works just fine for me

 

so maybe Hig Sierra changed something

Edited by juliosecco

Share this post


Link to post

Apologies, I should have known better on the version.

 

Pastebin, well I've learned something else new today:

 

https://pastebin.com/1yd87vLG

 

If I remove all the parameters and just have screencapture in the script then it does find the command and complains about the lack of file being specified.

Edited by MarcG

Share this post


Link to post

@MarcG Seems like the utility is still the same. Since your error is somewhat bizarre, this looks like an oddity in your personal setup. Just to get more data, what’s the result of: /usr/sbin/screencapture -i -P '/tmp/test.png'?

Share this post


Link to post
7 minutes ago, vitor said:

@MarcG Seems like the utility is still the same. Since your error is somewhat bizarre, this looks like an oddity in your personal setup. Just to get more data, what’s the result of: /usr/sbin/screencapture -i -P '/tmp/test.png'?

 

I prefer to think of it more as special, rather than odd!

 

Separating the -iP solves it, I don't need the path, and the temp filename works.

 

So I'm using: screencapture -i -P "$(mktemp).png"

 

Can't begin to tell you how much time this, and one simple text replacement is going to save me. Thank you.

Share this post


Link to post
5 minutes ago, juliosecco said:

just for curiosity and trying to understand,

 

could you try this?

 

snapshot2

 

 

 

 

This worked as expected once I'd inserted the desktop path.

Share this post


Link to post
22 minutes ago, MarcG said:

Separating the -iP solves it, I don't need the path, and the temp filename works.

 

So I'm using: screencapture -i -P "$(mktemp).png"

 

Great! Not sure why having them together is not working on 10.13, but what matters is that it’s now working for you.

 

Marking as solved. Enjoy! And welcome to the forums.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...