Jump to content

[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!

Link to comment

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

Link to comment

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
Link to comment

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
Link to comment
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?

Link to comment
  • vitor changed the title to [SOLVED] Screenshots to preview. Is this possible?
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.

Link to comment
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.

Link to comment
  • vitor changed the title to Screenshots to preview. Is this possible?

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

Link to comment
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?

Link to comment
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
Link to comment
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.

Link to comment
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.

Link to comment
  • vitor changed the title to [SOLVED] Screenshots to preview. Is this possible?

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
×
×
  • Create New...