got a Problem to solve unfortunalty I am sitting right now since 5 hours and can’t get it to work.
The problem first.
down below is the text which is safe to the clipboard.
Now the name varies all the time. except the name “ARTNR” this is consisting and we need the 11numbers after that, “123456-A123” the numbers after that change as well so the only possibility will be to track down the ARTNR and then copy the next 11 numbers from there one to the clipboard.
I experimented with trim
but endless to say it would not work right.
on trim(myString)
repeat until myString does not end with “ARTNR”
set myString to text 1 through -2 of myString
end repeat
end trim
has anyone a idea how to solve that?
the Text copied to clipboard is :
try this, it uses text item delimiters.
It’s also assumed that there’s always a space character after ARTNR which is omitted
set theText to get the clipboard
try
set {TID, text item delimiters} to {text item delimiters, "ARTNR "}
set articleNumber to text 1 thru 11 of text item 2 of theText
set text item delimiters to TID
set the clipboard to articleNumber
on error
set text item delimiters to {""}
display dialog "the text on the clipboard does not contain the string ARTNR"
end try
I’m not sure that I understand correctly the question.
If I does, this piece of code may help :
# Fill the clipboard to test the script
set the clipboard to "WEB 12 / 123 BLABA: ABCDE12 ID: 1234567890123
ABC ABCDEF
123456-A123
abcde - schwarz abcdefg
O ABCDE | OGHI | 0ABCDE | OQURSTU
*123456-A123*
ARTNR 123456-A123
*123456*
"
# The useful code starts here.
set theString to the clipboard as text
set stripped to my trim(theString)
set the clipboard to stripped
on trim(myString)
set {oTid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "ARTNR"}
text item 1 of myString
set myString to {result, ""} as text
set AppleScript's text item delimiters to oTid
return myString
end trim
The returned string is :
“WEB 12 / 123 BLABA: ABCDE12 ID: 1234567890123
ABC ABCDEF
123456-A123
abcde - schwarz abcdefg
O ABCDE | OGHI | 0ABCDE | OQURSTU 123456-A123
ARTNR”
set theText to get the clipboard
try
set {TID, text item delimiters} to {text item delimiters, "ARTNR "}
set articleNumber to text 1 thru 11 of text item 2 of theText
set text item delimiters to TID
set the clipboard to articleNumber
on error
set text item delimiters to {""}
display dialog "the text on the clipboard does not contain the string ARTNR"
end try
@yvan I tried yours as well unfortunately it sometimes gave me the wrong numbers. I couldn’t figure it out.
If the string “ARTNR” is not guaranteed to be completed by a trailing space you may use this variant:
set theText to get the clipboard
try
set {TID, text item delimiters} to {text item delimiters, {"ARTNR ", "ARTNR"}} # EDITED
set articleNumber to text 1 thru 11 of text item 2 of theText
set text item delimiters to TID
set the clipboard to articleNumber
on error
set text item delimiters to {""}
display dialog "the text on the clipboard does not contain the string ARTNR"
end try
If I’ve understood correctly and you are in fact extracting these strings from 11,000 files, I would use one of the Unix tools designed to find and return strings from files.
`cd` in the Terminal to the correct directory and run:
grep -Eho "^ARTNR *[0-9]{6}-[[:alnum:]]+" *
OR
sed -En '/^ARTNR *[0-9]{6}-[[:alnum:]]+/{ s!ARTNR *!!; p; }' *
OR
awk '/^ARTNR *[0-9]{6}-[[:alnum:]]+/ { print $2 }' *
Of course these will work fine from AppleScript using do shell script.
The result can be written to a file, to the clipboard, to a BBEdit or TextWrangler document, etc.
well it is just a part of the script. we are an e-comerce Photo operator and therefor we get articles with EAN codes. once a Article is received we need to scan the EAN and while our run sheet for all Fotostations Text and so on is being printed the script will grep the Art number from the openend and printed pdf and logs in a file for further processing (knowing what we got shipped to us) i just didn’t want to confuse you guys to much so i just descripte the port i could not figure out. right know it has more or less over 50 lines. if you like i share it with you. I also add the shell grep function works as well quite well didn’t had to much of knowledge of shell but i think i need to look into that kinda scripting at least it makes the script bit smaller.
What it does:
Looks if the name of marked file is “Lager”
renames the “Lager” into - gedrucked (=german 4 “printed”)
tells preview to print
greps the ARTNR (safes them to desktop log file foulder)
and thats it
another script is then sending all log files as text via mail at night. i figured it is easier for the people if it is as separate files since changes or destruction of articles need to be taken out before sending.
tell application "Finder"
set thisItem to selection as alias
if name of thisItem contains "LAGER" then
tell application "Finder"
set theFile to (thisItem as string) as alias
set theName to name of theFile
set the clipboard to (theName as string)
set the label index of thisItem to 1
set folder_name to the name of thisItem as string
set the clipboard to (the clipboard as string)
set the myText to the clipboard
set myText to text 6 thru -1 of myText
set the clipboard to (myText as string)
set the nameoftheperson to (get the clipboard)
set name of thisItem to ("gedruckt" & nameoftheperson)
open thisItem
activate application "Preview"
tell application "System Events"
tell process "Preview"
keystroke "a" using command down
keystroke "c" using command down
delay 0.2
click menu item -1 of menu 3 of menu bar 1
repeat until sheet 1 of window 1 exists
end repeat
keystroke return
delay 0.5
set articleNumber to do shell script "echo " & quoted form of (the clipboard) & " | grep -o -e [0-9][0-9][0-9][0-9][0-9][0-9]-[A-Z][0-9][0-9][0-9] | sort | uniq"
delay 0.5
keystroke "w" using command down
end tell
end tell
set txtpath to "your Path to log folder" & articleNumber
do shell script "echo " & quoted form of articleNumber & " > " & quoted form of txtpath
end tell
else
display dialog "wrong chosen file"
end if
end tell
your script is very complicated. The clipboard is not needed at all.
You could save partial results in normal local variables.
Consider that selection in Finder returns a list of objects.
A simple coercion to alias throws alway an error when multiple items are selected.
I recommend to use a repeat loop to process each selected file separately,
In the first part there is a lot of redundant code. It can be reduced to three lines.
tell application "Finder" to set selectedItems to (get selection)
repeat with anItem in selectedItems
set theName to name of anItem
set thisItem to anItem as alias
if theName contains "LAGER" then
tell application "Finder"
set the label index of thisItem to 1
set nameoftheperson to text 6 thru -1 of theName
set name of thisItem to ("gedruckt" & nameoftheperson)
open thisItem using application file id "com.apple.Preview"
end tell
set articleNumber to do shell script "echo " & quoted form of nameoftheperson & " | grep -o -e [0-9][0-9][0-9][0-9][0-9][0-9]-[A-Z][0-9][0-9][0-9] | sort | uniq"
activate application "Preview"
tell application "System Events"
tell process "Preview"
-- do the other stuff
end tell
end tell
end if
end repeat
It seems that this script is for a commercial purpose.
If yes, hire a professional programmer/scripter. It will eventually save a lot time and money