Swift : get text from safari (AppleScript) - swift

I have no experience with Swift , but my organisation don't want anyone to run AppleScript anymore.
I'll be alright using swift though
I was hopping to be able to get text from safari on a desktop swift app
for e.g
property leftEdge5 : "class=\"value\">"
property rightEdge5 : "</span>"
tell application "Safari"
set TheTransactionIDGraber to do JavaScript "document.getElementsByClassName('field emphasizedid')[0].innerHTML;" in current tab of window 1
end tell
set transactionID to ""
set theText to Unicode text
set theSource to TheTransactionIDGraber
try
set saveTID to text item delimiters
set text item delimiters to leftEdge5
set classValue to text item 2 of theSource
set text item delimiters to rightEdge5
set transactionID to text item 1 of classValue
set text item delimiters to saveTID
transactionID
end try
set the clipboard to "PID: " & transactionID
is they an easy way to do the same with Swift ? or get swift to run a basic applescript and store the value ? perhaps in a plist file

Related

How to pass argument for the apple script

I have following script :)
property word_docs : {"org.openxmlformats.wordprocessingml.document", "com.microsoft.word.doc"}
property default_path : (path to desktop) as alias
property Delim : {".docx", ".doc"}
property PDF : ".pdf"
set outPDF to {}
set selected_files to (choose file of type word_docs default location default_path with multiple selections allowed without invisibles and showing package contents)
set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, Delim}
repeat with afile in selected_files
copy item 1 of text items of (afile as text) & PDF to the end of outPDF
end repeat
set AppleScript's text item delimiters to TID
tell application id "com.microsoft.Word"
activate
repeat with i from 1 to count of selected_files
open (item i of selected_files)
set theOutputPath to (item 1 of outPDF)
-- close access (open for access exportDocument)
tell active document
save as it file name theOutputPath file format format PDF
close saving no
end tell
end repeat
end tell
return
That helps me convert doc & docx files -> pdf, but it is too interactive.
And I have idea to run this script via terminal and i want to pass file path or directory as argument
for example:
$ script /Users/test/dest_dir/ /Users/test/out_dir/
will produce all pdf files into out_dir.
I saw this library also but it converts only docx files:
https://pypi.org/project/docx2pdf/
Is there anyone here who can help me rewrite this script .. I don't understand this language at all. or maybe someone will point to the finished tool. I need to do this on the mac os operating system.
see http://hints.macworld.com/article.php?story=20050523140439734
argv is an array of arguments in string format
on run argv
return item 1 of argv

VBA Label formatting

I am using label from tool box control to place and write a default text to my form. However I want to break line and I don't know how to do it.
I don't believe writing a paragraph on the caption field is the right way to customize my form, is it? Am I missing something?
When you are writing your text into your label press (Shift+Enter) to add a return carriage.
You could use something like the vbCrLf constant to update the caption by code:
Label1.Caption = "New" & vbCrLf & "caption"
You can use 'vbnewline' to move to the next line in the field, just make sure your label is sized properly so it will show.
Example: string = "Text" & vbnewline & "text"
will read:
Text
text

Need a fix for an Applescript bug in my effort to determine existence of renamed file

I have a script to copy all email attachments in a selected set of emails to a folder after some renaming. Because the attachments sometimes have identical names at origin, even with renaming, I need to add something like " copy" to subsequent versions so they don't save atop one another. With some programming knowledge but very little understanding of AppleScript, I cobbled this together:
tell application "Mail"
set theMessages to selection
set theOutputFolder to (choose folder) as string
repeat with a from 1 to length of theMessages
set theMessage to item a of theMessages
set {year:y, month:m, day:d} to date sent of theMessage
set theDate to (y * 10000 + m * 100 + d) as string
set theAttachments to every mail attachment of theMessage
repeat with b from 1 to length of theAttachments
set theAttachment to item b of theAttachments
set theAttachmentName to theDate & " " & name of theAttachment
set theSavePath to theOutputFolder & theAttachmentName
tell application "System Events" to exists file theSavePath
repeat while the result
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"."}
set delimitedList to every text item of theSavePath
set suffix to "." & last item of delimitedList
try
copy text items 1 thru -2 of theSavePath to theSavePathBase
on error
copy theSavePath to theSavePathBase
end try
-- display dialog "theSavePath pre- : " & theSavePath
-- display dialog "theSavePathBase & ' copy' & suffix pre- " & theSavePathBase & " copy" & suffix
set AppleScript's text item delimiters to oldDelims
copy theSavePathBase & " copy" & suffix to theSavePath
-- display dialog "theSavePath post- : " & theSavePath
tell application "System Events" to exists file theSavePath -- <<< *** BOMBS HERE
-- display dialog "Made it past existence check."
end repeat
try
display dialog "preparing to save..."
save theAttachment in theSavePath
on error errText number errNum
display dialog errText
end try
end repeat
end repeat
end tell
It works, except that when the need to fix the name of a copy is encountered, it bombs at the indicated location with this message: "System Events got an error: Can’t make {"...Desktop:Mail Attachments:20140830 Resumé 2014", "pages", " copy", ".zip"} into type integer." Something about the way I reconstituted the name changed the type to something the exists checker can't handle.
Any help would be appreciated. Help with an explanation would be better, since I'm not an AppleScripter but am interested. Thanks!
Your error message is telling you the problem. Notice the brackets {} around the error message.
Can’t make {"...Desktop:Mail Attachments:20140830 Resumé 2014", "pages", " copy", ".zip"} into type integer."
That's indicating that this is a list of items, not a string. As such you need to make it a string first therefore change...
copy text items 1 thru -2 of theSavePath to theSavePathBase
to:
copy (text items 1 thru -2 of theSavePath) as text to theSavePathBase
With that being said, I don't think your actual copy command (as follows) will work. It seems you're wanting the copy command to rename the file and copy it all in one step.
copy theSavePathBase & " copy" & suffix to theSavePath
The copy command won't be able to find "...Desktop:Mail Attachments:20140830 Resumé 2014.pages. copy.zip" because it doesn't exist. That's the name you want for the renamed file. I think you're best approach would be to use the "cp" unix executable command to preform your copy because it can copy and rename in one step. Something like this although you'll have to figure out how to get the actual value for the variable "theAttachmentCurrentPath".
do shell script "cp " & quoted form of POSIX path of theAttachmentCurrentPath & space & quoted form of POSIX path of (theOutputFolder & text 1 thru -5 of theAttachmentName & " copy" & text -4 thru -1 of theAttachmentName)

Copy and Paste text from Dialogue Box in Applescript

i'm writing a script that needs to copy and paste text from an entry in a dialogue box
set query to text returned of (display dialog "Enter Text" default answer "" buttons {"Input", "Cancel"} default button 1)
query
tell application "System Events"
keystroke "c" using command down
keystroke "v" using command down
end tell
when i run this script it copies and pastes "set query to text..."
how can I copy the query that I entered ? i'm going to paste the text somewhere else later but I need to figure out how to actually copy the text. I was using keystroke query but that was taking a long time when the strings were long
Use set the clipboard:
display dialog "" default answer ""
set the clipboard to text returned of result
If others get here searching for how to for example copy a long error message from a dialog shown by OS X, you can use Accessibility Inspector, which is in /Applications/Xcode.app/Contents/Applications/.

iPhone - How can I right align PSTextFieldSpecifier of settings bundle

I've settings bundle in my iPhone application. How can I right align the PSTextFieldSpecifier value?
This worked for me. If you open the property list (Root.plist) file as a source and paste the following for each entry you want right-aligned:
<key>IASKTextAlignment</key>
<string>IASKUITextAlignmentCenter</string>
Have the same desire. What I currently do is to put some space bars after my title. So I have to text fields in my settings, Account and Password:
Account TheName
Password *****
now I would count the chars that I need to get the same length of both strings. In this case the "Account" label has one char less then the "Password" label. So I would put the remaining characters count twice and append them to the label. In this case the label would be "Account " (2x " ")
following I will replace the " " with "_" so that you can see the difference
Account__ TheName
Password *****
It looks different sized in your XCode Settings Bundle but on the device or in Simulator it will be fine