My AppleScript is producing JSON instead of a plain string. Here's the code.
set lok to input2 & "/Contents/"
input2 is a variable that becomes a string. I get this error:
Can’t make {"[whatever input2 was]", "/Contents/"} into type string.
I don't understand what is going on. I did not call anything. When I call input2 by itself, it gives me it in plain text. Does anyone know what is going on here?
That's not a json, it's an AppleScript list.
The & operator adds another object to the list. If you want the list as text you need to coerce it "as text" like so
set lok to (input2 & "/Contents/") as text
It's not JSON (obviously because it isn't JS), it's an AppleScript list that when you execute & it adds another object to the list. To return text, coerce it "as text" like so:
set lok to (input2 as text) & "/Contents/"
Related
I'm looking for a way via AppleScript to take 11 symbols after ?v= part in an URL that's in clipboard and save them into a variable. Here's an example of a link:
https://www.youtube.com/watch?v=PxDK95Q5qN0&ab_channel=TheLateShowwithStephenColbert
So I need a little script that would take a link in clipboard, identify 11 symbols after ?v=, take those 11 symbols (PxDK95Q5qN0) from that link and save it into a variable (e.g. extract).
I did find a partial solution here — but that one only works when it's BETWEEN two parts e.g. ?v= and &. The problem with that solution is that many links are short and do not have the last & symbol (e.g. https://www.youtube.com/watch?v=PxDK95Q5qN0)
I'd appreciate any help or pointers here! :]
Whether you set the clipboard to, e.g.,:
set the clipboard to "https://www.youtube.com/watch?v=PxDK95Q5qN0&ab_channel=TheLateShowwithStephenColbert"
Or, e.g.,:
set the clipboard to "https://www.youtube.com/watch?v=PxDK95Q5qNO"
The following example AppleScript code returns the eleven characters after ?v=:
set extract to do shell script ¬
"sed -Ee 's/.*[?]v[=]([a-zA-Z0-9_-]{11}).*/\\1/'<<<" & ¬
(the clipboard as text)'s quoted form
Note that \\?v\\= can be used instead of the [?]v[=] portion of the sed command as in either case the ? and = are treated as regular characters instead of shell special characters.
Additionally, if the number of target characters changes from 11 yet ?v= and & are still in play, as in your examples, then this regex, with explanation shown further below, handles it:
.*[?]v[=]([a-zA-Z0-9_-]+[^&]).*
Notes:
If the 11 target characters contain other than what is shown in the capturing group:
([a-zA-Z0-9_-]{11})
Then modify it as needed.
Using info from https://regex101.com to explain the regex used in the sed command:
The main part of interest is the Capturing Group ([a-zA-Z0-9_-]{11}), as this is what's returned, if it exists directly after ?v= in the URL.
When Capturing Group is: ([a-zA-Z0-9_-]+)
That macsripter link points you in the right direction (though it's a lot of information to digest). As long as you're running 10.6 or later, you can use this routine:
on getTheV(link_text)
set {tid, my text item delimiters} to {my text item delimiters, {"=", "&"}}
set theV to second text item of link_text
set my text item delimiters to tid
return theV
end getTheV
which breaks the link up into text items — split on '=' and '&' — and returns the second item.
Use it like so:
set theV to getTheV("http://...")
Both the links you provided return the correct value.
i have a string "Travel & Hospitality". like this.
When i sent it through XML API the output is like below.
"Travel & Hospitality"
i tried removing it with ruby code like below and sent it through XML.
"Travel & Hospitality".gsub("&","&").
<Specialization__c>Travel & Hospitality</Specialization__c>
even though gsub is removing "amp;" again while sending it through XML tags again the amp; word is coming.
How can i remove it.my desired output is
"Travel & Hospitality"
XML doesn't allow such a thing. & is not allowed to appear unescaped.
You could have an XML file like this instead:
<Specialization__c><![CDATA[Travel & Hospitality]]></Specialization__c>
That would work, but the problem is how to convince your XML output library to do something like that. It might not even be possible at all. (I might be wrong about that last part. I know nothing about ruby)
I'm trying to grab data from an AS400 screen & populate an email using that data but seem to have bumped into something I'm struggling to overcome. Here's a slice of what I have so far:
Dim polNo
polNo = GetText(10,18,10)
Dim wsh
Set wsh=CreateObject("WScript.Shell")
subSub1_()
sub subSub1_()
// Just doing this to check the text I have
SendKeys(polNo)
// Sent the eMail with the text
wsh.Run "mailto:testing#somemailbox.com?Subject=" & polNo
end sub
With the above, the resulting email subject line takes only the first word upto the first space. From what I've found, this is a parsing issue & have discovered the following line that should help.
polNo = Chr(34) + Replace(polNo,chr(34),chr(34)&chr(34))
The above line places all of the text in quotes (I know this because my SendKeys line now shows the GetText result with a " at the start.
The issue is when I reach the mailto line as Outlook pops up a window saying:
"The command line argument is not valid. Verify the switch you are using."
My end result will be an email that has a subject & a body with text taken from various parts of the screen.
Solved: Thanks to dmc below, he started me on the right line.
However, the solution was not to use Chr(34) but to use something as simple as:
polNo = Replace(polNo," ","20%")
Although it might not look like it, you're constructing a URL. As such, the contents of that URL must be URL Encoded. Certain characters can't be included in a URL, including a space. Those characters are represented with a percent sign followed by the ASCII code of the character in hexadecimal. For example, a space is changed to %20.
See the link below for a VBScript routine that will URL encode and decode strings.
http://www.justskins.com/forums/wsh-equivalent-of-server-38778.html
Edit: Although this is commonly known as URL encoding, the thing you're constructing is technically a URI. Wikipedia has a good page that explains further.
First, I'm using Swift. Second this line works fine in my code:
let didIt = fileManager.moveItemAtURL(originalFilePath, toURL: newFilePath, error: nil)
...as long as there are no special characters in the newFilePath. if the newFilePath has a dollar sign or an ampersand ($, & ) in it, the line fails. My issue is that the newFilePath comes from a text field in a window where the user can type any old thing. How do I escape special characters, or encode them so they will pass the test and be included in the new filename?
thanks in advance for any pointers.
My issue is that the newFilePath comes from a text field in a window where the user can type any old thing.
Right there is your problem. Why are you not using an NSSavePanel for letting the user select a name under which to save a file?
If you insist on taking input from a text field, the docs for -URLByAppendingPathComponent: specifically say that the path component string should be "in its original form (not URL encoded)" (emphasis mine).
How did you originally create newFilePath, before appending the path component? For example, you should have used one of the methods with "[fF]ileURL" in the name.
I have a VBScript file that accepts a variety of parameters, one of which is a text string from a varchar(max) field in SQL Server, which is used in the script as the .HTMLBody of an e-mail message.
However, when the e-mail message is created and displayed, all of the text is jumbled together, making it somewhat difficult to read since it ignores all line breaks.
The actual script is called from a Java application like this:
Runtime.getRuntime().exec(arguments)
Where arguments is a list of arguments pass into the VBScript file, which is also part of the arguments string like this:
wscript filepath\sendEmail + arguments
Each argument is held in quotes to segment each unique parameter in the VBScript file, which is structured like this:
Set objArgs = WScript.Arguments
Arg1 = objArgs(0)
...
Set MyApp = CreateObject("Outlook.Application")
...
.HTMLBody = Arg8
Many thanks for any help you can provide.
-Brian
According to this doc for HTMLBody Property, .HTMLBody deals with HTML:
.HTMLBody = "<HTML><BODY>Enter the message text here. </BODY></HTML>"
As whitespace has reduced significance in HTML, you should replace (whatever) line-endings of your input text with <br> (or <br />).