Jump to content

Split argument in applescript


Recommended Posts

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.

Link to comment

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.

Link to comment

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.

Link to comment

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

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

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

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

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...