MarcG Posted May 18, 2018 Share Posted May 18, 2018 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
deanishe Posted May 18, 2018 Share Posted May 18, 2018 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: Run screencapture Activate Preview Simulate ⌘N to create new image from Pasteboard Link to comment
juliosecco Posted May 18, 2018 Share Posted May 18, 2018 (edited) 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 May 18, 2018 by juliosecco Link to comment
vitor Posted May 18, 2018 Share Posted May 18, 2018 (edited) 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 May 18, 2018 by vitor MarcG, deanishe and juliosecco 2 1 Link to comment
MarcG Posted May 18, 2018 Author Share Posted May 18, 2018 Seriously impressed. What an awesome forum. Many thanks! vitor 1 Link to comment
MarcG Posted May 18, 2018 Author Share Posted May 18, 2018 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 Posted May 18, 2018 Share Posted May 18, 2018 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
MarcG Posted May 18, 2018 Author Share Posted May 18, 2018 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 Posted May 18, 2018 Share Posted May 18, 2018 Open the debugger, set the log output to “All Information”, and post the output. Run the command in a Terminal. What happens? What is your versions of Alfred? What is your versions of macOS? Link to comment
MarcG Posted May 18, 2018 Author Share Posted May 18, 2018 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
vitor Posted May 18, 2018 Share Posted May 18, 2018 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
juliosecco Posted May 18, 2018 Share Posted May 18, 2018 (edited) 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 May 18, 2018 by juliosecco Link to comment
MarcG Posted May 18, 2018 Author Share Posted May 18, 2018 (edited) 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 May 18, 2018 by MarcG Link to comment
vitor Posted May 18, 2018 Share Posted May 18, 2018 @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'? MarcG 1 Link to comment
juliosecco Posted May 18, 2018 Share Posted May 18, 2018 just for curiosity and trying to understand, could you try this? snapshot2 MarcG 1 Link to comment
MarcG Posted May 18, 2018 Author Share Posted May 18, 2018 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
MarcG Posted May 18, 2018 Author Share Posted May 18, 2018 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. Link to comment
vitor Posted May 18, 2018 Share Posted May 18, 2018 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. MarcG 1 Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now