AHK can't send python code?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
barracuda
Posts: 5
Joined: 16 Jan 2019, 20:00

AHK can't send python code?

16 Jan 2019, 20:07

Due to various issues preventing me from directly sending a python file into a docker container, I have been looking for workarounds, the most promising of which is to copy the entire code into an AHK clipboard script.

However, I always get errors about missing close brackets or quotations. I understand the code itself has formatting that may clash with AHK, so is there a way to strictly store a long block of text as a string, or other alternative solutions?

A snippet of my code is shown below, this is one that complains about no closing quotes:

Code: Select all

f1::
 {
   temp := clipboardall
   clipboard := "import argparse
import base64
import json
import re
import socket
import sys
import time

from google.cloud import pubsub_v1

# def receive_messages_with_custom_attributes(project_id, subscription_name):
#     """Receives messages from a pull subscription."""
#     # [START pubsub_subscriber_sync_pull_custom_attributes]
#     import time
#
#     from google.cloud import pubsub_v1
#
#     project_id = "sdfsdf"
#     subscription_name = "asr243"
#
#     subscriber = pubsub_v1.SubscriberClient()
#     subscription_path = subscriber.subscription_path(
#         project_id, subscription_name)
#
#     def callback(message):
#         print('Received message: {}'.format(message.data))
#         if message.attributes:
#             print('Attributes:')
#             for key in message.attributes:
#                 value = message.attributes.get(key)
#                 print('{}: {}'.format(key, value))
#         message.ack()
#
#     subscriber.subscribe(subscription_path, callback=callback)
#
#     # The subscriber is non-blocking, so we must keep the main thread from
#     # exiting to allow it to process messages in the background.
#     print('Listening for messages on {}'.format(subscription_path))
#     while True:
#         time.sleep(60)
#     # [END pubsub_subscriber_sync_pull_custom_attributes]

###############################################################################
import subprocess

)"

   sendinput, ^v
   clipboard := temp
 }
return

F7::
	Clipboard := pasteContent
	Send, ^V
	Clipboard :=
return
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: AHK can't send python code?

16 Jan 2019, 20:18

Code: Select all

Clipboard = 
(
import argparse
import base64
import json
import re
import socket
import sys
import time

from google.cloud import pubsub_v1

# def receive_messages_with_custom_attributes(project_id, subscription_name):
#     """Receives messages from a pull subscription."""
#     # [START pubsub_subscriber_sync_pull_custom_attributes]
#     import time
#
#     from google.cloud import pubsub_v1
#
#     project_id = "sdfsdf"
#     subscription_name = "asr243"
#
#     subscriber = pubsub_v1.SubscriberClient()
#     subscription_path = subscriber.subscription_path(
#         project_id, subscription_name)
#
#     def callback(message):
#         print('Received message: {}'.format(message.data))
#         if message.attributes:
#             print('Attributes:')
#             for key in message.attributes:
#                 value = message.attributes.get(key)
#                 print('{}: {}'.format(key, value))
#         message.ack()
#
#     subscriber.subscribe(subscription_path, callback=callback)
#
#     # The subscriber is non-blocking, so we must keep the main thread from
#     # exiting to allow it to process messages in the background.
#     print('Listening for messages on {}'.format(subscription_path))
#     while True:
#         time.sleep(60)
#     # [END pubsub_subscriber_sync_pull_custom_attributes]

###############################################################################
import subprocess

)
usually a block should look like this
barracuda
Posts: 5
Joined: 16 Jan 2019, 20:00

Re: AHK can't send python code?

16 Jan 2019, 20:40

Ok so that will make AHK complain about closing brackets being not recognized, which I circumvented by putting all closed brackets in lines with prior text, instead of a line having only a closed bracket.

But now it's even more mysteriously complaining about

Code: Select all

'overwroteGeneration'
containing an illegal character. This was from part of the code that wasn't included in the snippet above, but it's not the first nor last time there is code in single quotations, and I'm pretty sure it's just utf-8 plaintext.

Any idea how to get around this?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: AHK can't send python code?

16 Jan 2019, 21:48

post everything. likely u got a problem with the escaping
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: AHK can't send python code?

16 Jan 2019, 23:32

barracuda wrote:
16 Jan 2019, 20:40
Ok so that will make AHK complain about closing brackets being not recognized, which I circumvented by putting all closed brackets in lines with prior text, instead of a line having only a closed bracket.

But now it's even more mysteriously complaining about

Code: Select all

'overwroteGeneration'
containing an illegal character. This was from part of the code that wasn't included in the snippet above, but it's not the first nor last time there is code in single quotations, and I'm pretty sure it's just utf-8 plaintext.

Any idea how to get around this?
Swagfag is correct. Unless people can see the code, it's very difficult for them to tell you what might be going wrong.

If you don't want to show all of your code, then you can make and post a smaller sample version of your code that contains the same problem. This way the more experienced AutoHotkey programmers can take a look at it and give you suggestions.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: AHK can't send python code?

19 Jan 2019, 03:13

- See CHARACTERS (VERBATIM), here:
jeeswg's characters tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=26486
- One possible reason for your problem is that AutoHotkey may have interpreted parentheses intended as text, and seen them as ending the continuation section. And so anything after that was viewed as code, as invalid code.
- There is more info about continuation sections here, e.g. comments (semicolons) and parentheses could be issues.
Scripts - Definition & Usage | AutoHotkey
https://autohotkey.com/docs/Scripts.htm#continuation
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: AHK can't send python code?

19 Jan 2019, 04:43

Like jeeswg pointed out it's about escaping. Escaping closing parenthesis and quotes works:

Code: Select all

MsgBox % "
(
import argparse
import base64
import json
import re
import socket
import sys
import time

from google.cloud import pubsub_v1
# def receive_messages_with_custom_attributes(project_id, subscription_name`):
#     """"""Receives messages from a pull subscription.""""""
#     # [START pubsub_subscriber_sync_pull_custom_attributes]
#     import time
#
#     from google.cloud import pubsub_v1
#
#     project_id = ""sdfsdf""
#     subscription_name = ""asr243""
#
#     subscriber = pubsub_v1.SubscriberClient(`)
#     subscription_path = subscriber.subscription_path(
#         project_id, subscription_name`)
#
#     def callback(message`):
#         print('Received message: {}'.format(message.data`)`)
#         if message.attributes:
#             print('Attributes:'`)
#             for key in message.attributes:
#                 value = message.attributes.get(key`)
#                 print('{}: {}'.format(key, value`)`)
#         message.ack(`)
#
#     subscriber.subscribe(subscription_path, callback=callback`)
#
#     # The subscriber is non-blocking, so we must keep the main thread from
#     # exiting to allow it to process messages in the background.
#     print('Listening for messages on {}'.format(subscription_path`)`)
#     while True:
#         time.sleep(60`)
#     # [END pubsub_subscriber_sync_pull_custom_attributes]

###############################################################################
import subprocess
)"
To avoid having to escape characters you could put your template in a different file and use FileRead.
list
Posts: 222
Joined: 26 Mar 2014, 14:03
Contact:

Re: AHK can't send python code?

19 Jan 2019, 04:49

If you have a lot of those snippets you could try Lintalist - https://www.autohotkey.com/boards/viewt ... f=6&t=3378 :-)
barracuda
Posts: 5
Joined: 16 Jan 2019, 20:00

Re: AHK can't send python code?

24 Jan 2019, 01:25

Sorry about my absence, been busy putting out a figurative fire with the servers.

While we were in there, I took a closer look at our enterprise network configuration and found the reason why I couldn't share drives with docker in the first place, and found a work-around.

I don't really need to do this via AHK anymore, but thank you all for the very warm responses and helpful advice, this has been one of the friendliest t̶e̶c̶h̶-̶r̶e̶l̶a̶t̶e̶d̶ forums I've seen in many years.

Cheers!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, Joey5, mikeyww, Ralf_Reddings200244 and 291 guests