drowning_not_waving Posted July 3, 2014 Posted July 3, 2014 Hi all. I am home-schooled Applescripter. I’m tryin to make an Applescript workflow that splits an argument in half and pastes one half first and the other half later. I am using a text box with a Run Script (osa script) output. The problem is getting the applescript to “see” the argument and parse/explode it. The set clipboard / paste functions work fine. on run argv set query to argv as text set AppleScript's text item delimiters to {"."} tell application "Microsoft Word" set the clipboard to text item 1 of argv activate end tell delay 2 tell application "System Events" keystroke "v" using command down end tell delay 2 tell application "Microsoft Word" set the clipboard to text item 2 of argv activate tell application "System Events" keystroke "v" using command down end tell end tell end run Any help much appreciated.
vitor Posted July 3, 2014 Posted July 3, 2014 Not sure I understood your problem correctly. Is your issue with applescript, or with making this work specifically with Alfred? The former would make this a non-Alfred-specific question, so you should ask it somewhere with more visibility, like Stack Overflow. You’ll likely get better, faster, and more diverse answers. If the latter, could we take a look at the workflow? This would be useful to understand exactly what you want — calling this via a Hotkey, has different implications (including on getting text selection) than calling it with a Keyword, for example All that said, there’s no reason we can’t try to solve your issue here, even if it only pertains to applescript. I’ll point out, though, that if the task at hand does not demand applescript, you should consider using something else — it will very likely be a better (time) investment for future needs. You should also avoid the “hack” of pressing keystrokes to accomplish tasks. While it might work, it’s prone to unforeseen errors and possible oddities. Not sure there’s an alternate native applescript command for that, though.
drowning_not_waving Posted July 3, 2014 Author Posted July 3, 2014 Hi Vitor. It relates to an Alfred script I'm working on (using a text box and osascript output). I understand Applescript may not be the ideal language but at least I know a bit of it. I am not an IT person (believe), I'm just trying to learn a few modest skills to make some hacky Alfred workflows. If I can learn how to get multiple queries to work, that will just about do me for programming. I'll leave the rest to the experts. Thanks.
smarg19 Posted July 3, 2014 Posted July 3, 2014 As Vitor said, more information is better. But here are some general thoughts on the code posted: ALWAYS reset Applescript's text item delimiters. If you don't, that will carry over to other scripts (which is bad, bad, bad). Use a more unique delimiter in your input. A period (".") is far too generic. Since I don't know exactly what types of things are likely to be input, I can't be more specific with suggestions, but in general, you want to concatenate using something that wouldn't otherwise be in a string (e.g. "|.|"). You have slightly misunderstood how ASTIDs work. You can't set them globally and then ask for text items of argv. Here's sample code that properly uses ASTIDs: set query to argv as text --Save default value of ASTID for later reset. set astid to Applescript's text item delimiters --Use a unique delimiter set Applescript's text item delimiters to {"|.|"} --Split the query string, not the argv list set front_string to text item 1 of query set back_string to text item 2 of query --Reset ASTID set Applescript's text item delimiters to astid --Now you can use front_string and back_string as needed
smarg19 Posted July 3, 2014 Posted July 3, 2014 Here's a possible rewrite of your code: on run argv set query to argv as text set astid to Applescript's text item delimiters set AppleScript's text item delimiters to {"|.|"} set front_string to text item 1 of query set back_string to text item 2 of query --Reset ASTID set Applescript's text item delimiters to astid --Paste first part tell application "Microsoft Word" set the clipboard to front_string activate delay 2 tell application "System Events" to keystroke "v" using command down delay 2 set the clipboard to back_string activate tell application "System Events" to keystroke "v" using command down end tell end run
drowning_not_waving Posted July 3, 2014 Author Posted July 3, 2014 (edited) Hi Smarg. I'm sorry if I'm not explaining it properly and thank you for your help. What I want to be able to do is: 1. call up Alfred workflow (hotkey or keyword) 2. enter argument text "X.Y" (using "." or " " or whatever as text delimiter) 3. script opens application, e.g Word 4. script pastes/keystrokes item 1 of argument ("X") into Word document 5. script tabs/returns to another part of the document and pastes/keystrokes item 2 of argument ("Y"). I have tried your script and unfortunately it didn't work. It got stuck on the same line as my script: "set query to argv as text" I know that because if I replace "set query to argv as text" with "set argv to "X.Y"" it works (in the sense that it pastes "X" and "Y" separately). But I just can't get the script to take the argument "X.Y" (in an Alfred dialog) and do the same thing. Again, thanks. Edited July 3, 2014 by drowning_not_waving
smarg19 Posted July 3, 2014 Posted July 3, 2014 That's odd, because I've made a test workflow and it works just fine. Try downloading my test workflow. Look at the script and test it out using the `tester` keyword. (Make sure you have a Microsoft Word document already open). Let me know how that goes. https://www.dropbox.com/s/r4t68cxcn3qj4vf/tester.alfredworkflow
drowning_not_waving Posted July 3, 2014 Author Posted July 3, 2014 LEGEND. It appears to work (when I use your workflow). Maybe it didn't work before because I didn't use shell script stuff/workflow folder, I just pasted the script straight into the output. This has to be about the most useful forum on the internets. Thanks smarg19 and Vitor! smarg19 1
smarg19 Posted July 3, 2014 Posted July 3, 2014 For what it's worth, here's how the script should look if you put it into a "Run NSAppleScript" action: on alfred_script(q) set query to q as text set astid to AppleScript's text item delimiters set AppleScript's text item delimiters to {"."} set front_string to text item 1 of query set back_string to text item 2 of query --Reset ASTID set AppleScript's text item delimiters to astid --Paste first part tell application "Microsoft Word" set the clipboard to front_string activate delay 2 tell application "System Events" to keystroke "v" using command down delay 2 set the clipboard to back_string activate tell application "System Events" to keystroke "v" using command down end tell end alfred_script drowning_not_waving 1
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