Can some kind person tell me how to append text to the end of a file using Applescript? I used the Script Editor recorder to create a macro that helps me clean up some HTML tags. Now I want the macro to go to the end of the file and write a sentence. Is there a 'goto eof' command in Applescript?
Thanks!
tell application "TextWrangler"
activate
open find window
replace "<b>" using "<strong>" searching in text 1 of document 1 options {search mode:grep, starting at top:true}
replace "</b>" using "</strong>" searching in text 1 of document 1 options {search mode:grep, starting at top:true}
replace "<i>" using "<em>" searching in text 1 of document 1 options {search mode:grep, starting at top:true}
replace "</i>" using "</em>" searching in text 1 of document 1 options {search mode:grep, starting at top:true}
replace "<span>" using "" searching in text 1 of document 1 options {search mode:grep, starting at top:true}
replace "</span>" using "" searching in text 1 of document 1 options {search mode:grep, starting at top:true}
replace " " using "" searching in text 1 of document 1 options {search mode:grep, starting at top:true}
close find window
goto eof
write "hello world!"
end tell
Here's how you can do this in TextWrangler:
tell application "TextWrangler"
select insertion point after last character of text window 1
set selection to "hello world!"
end tell
The syntax will be unique to which text app you are using. Plus, you can also append text to a file using just applescript without opening the file in another app. (just search for "applescript append text")
Related
I'm looking for a script that will save the url's of my open tabs in Safari to a text document, so that I can open them later (its 10+ tabs). I found this script online:
[applescript]
tell application "Safari"
--Variables
set windowCount to number of windows
set docText to ""
--Repeat for Every Window
repeat with x from 1 to windowCount
set tabcount to number of tabs in window x
--Repeat for Every Tab in Current Window
repeat with y from 1 to tabcount
--Get Tab Name & URL
set tabName to name of tab y of window x
set tabURL to URL of tab y of window x
set docText to docText & "" & tabName & "" & linefeed as string
end repeat
end repeat
end tell
--Write Document Text
tell application "TextEdit"
activate
make new document
set the text of the front document to docText
end tell
[/applescript]
But when I try to run this I get the error
Expected expression, but found "/"
and it marks the last line, [/applescript] as being wrong. I've searched for the error but can't seem to find it. anyone got an idea?
You probably should remove the [applescript] and [/applescript] lines when you put the script into the AppleScript Editor.
Remove [applescript] and [/applescript]
I have a question and maybe someone can point me in the right direction.
I would like to be able to copy Text (mostly from "Get Info") and replace all the “.” with a space excluding the “.”before the file extension using AppleScript or Automator. I'd like to use this service when i right click and select the script or automator service.
E.G
Contraband.2012.DVDRip.htif-NYsIC.avi
will turn to
Contraband 2012 DVDRip htif-NYsIC.avi
I've search everywhere on this site and various places but I just cant seem to find what i need. I don’t know where to start writing this script. Is what i described even possible in apple script? I just need to be pointed in the right direction and I'll try and figure the rest out I'm just at a loss right now.
Thank's in advance.
You could use a Run Shell Script action like this:
for f; do
base=${f##*/}
[[ $base =~ .+\..+ ]] || continue
noext=${base%.*}
mv "$f" "${f%/*}/${noext//./ }.${base##*.}"
done
Set "Pass input" to "as arguments". ${f##*/} removes the longest */ pattern from the start of f and ${f%/*} removes the shortest /* pattern from the end of f.
You could also use a Run AppleScript action like this:
on run {input}
repeat with f in input
set text item delimiters to "."
tell application "Finder"
set ti to text items of (get name of (f as alias))
if number of ti > 2 then
set AppleScript's text item delimiters to " "
set name of (f as alias) to (items 1 thru -2 of ti as text) & "." & item -1 of ti
end if
end tell
end repeat
end run
The deal is this:
I use http://www.clipmenu.com/ ClipMenu to have 20 states of the clipboard, because i need to copy each line of some txt file separated. So i open the txt file, and i go through every line hitting command+shift+→ then command+c then ↑ and so on until i reach the top and i have all the lines copied and stored in the history of ClipMenu.
My question is, is there a way to make a service or script that copies every single line in an automated way? i think i could make a script that repeat those keystrokes until it reaches the top of the txt file but i have no idea how to make it so.
Thanks a lot.
I do not know how to do this using Automator, but using mono [MonoMac] it is very simple:
using (System.IO.FileStream file = new System.IO.FileStream("path", System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)) {
using (System.IO.StreamReader reader = new System.IO.StreamReader(file)) {
while (!reader.EndOfStream) {
String line = reader.ReadLine ();
System.Windows.Forms.Clipboard.SetText(line);
}
}
}
Try:
set fileText to read (choose file) as «class utf8»
set filePara to paragraphs of fileText
repeat with aPara in filePara
set aPara to contents of aPara
if aPara ≠ "" then set the clipboard to aPara
end repeat
ClipMenu seems to ignore "transient" clipboards, so you also need a delay between the copy actions:
read POSIX file "/Users/username/Documents/test.txt" as «class utf8»
repeat with p in reverse of paragraphs of result
if contents of p is not "" then set the clipboard to contents of p
delay 1
end repeat
Or using UI scripting:
delay 1
tell application "TextEdit"
activate
set n to number of paragraphs of document 1
end tell
tell application "System Events"
key code {125, 123} using command down
repeat n times
key code 124 using {shift down, command down}
keystroke "c" using command down
key code 126
delay 1
end repeat
end tell
The key codes are listed in Events.h.
I have a folder containing about 5000 files with names like:
Invoice 10.1 (2012) (Digital) (4-Attachments).pdf
Carbon Copy - Invoice No 02 (2010) (2 Copies) (Filed).pdf
01.Reciept #04 (Scanned-Copy).doc
I want to rename these files by removing everything from the first bracket onwards, so they look like this:
Invoice 10.1.pdf
Carbon Copy - Invoice No 02.pdf
01.Reciept #04.doc
I have found lots of scripts that will remove the last x letters, but nothing that will crop from a particular character.
Ideally I would like to use Automator, but I'm guess this might too complex for it. Any ideas?
Try:
set xxx to (choose folder)
tell application "Finder"
set yyy to every paragraph of (do shell script "ls " & POSIX path of xxx)
repeat with i from 1 to count of yyy
set theName to item i of yyy
set name of (file theName of xxx) to (do shell script "echo " & quoted form of theName & " | sed s'/ (.*)//'")
end repeat
end tell
The code posted by #adayzone will work, but there is no need to use sed for this – plain AppleScript will do, using offset:
set fullString to "Invoice 10.1 (2012) (Digital) (4-Attachments).pdf"
set trimmedString to text 1 thru ((offset of "(" in fullString) - 1) of fullString
-- trim trailing spaces
repeat while trimmedString ends with " "
set trimmedString to text 1 thru -2 of trimmedString
end repeat
this returns “Invoice 10.1". To split the file name into the name and extension, and re-add the extension, you can use System Events’ Disk-File-Folder suite, which will provide the handy name extension property you can store and re-add after trimming the name.
Assuming you use some Automator action to get the files to be processed, the full processing workflow would be to add an AppleScript action after the file selection part with the following code:
repeat with theFile in (input as list)
tell application "System Events"
set theFileAsDiskItem to disk item ((theFile as alias) as text)
set theFileExtension to name extension of theFileAsDiskItem
set fullString to name of theFileAsDiskItem
-- <insert code shown above here>
set name of theFileAsDiskItem to trimmedString & "." & theFileExtension
end tell
end repeat
If you want your Automator workflow to process the files any further, you will also have to create a list of aliases to the renamed files and return that from the AppleScript action (instead of input, which, of course, is not valid anymore).
I've got a choose file in my AppleScript. When I run the script and choose a file, the output is always the full file path with the file extension on the end. For example:
Macintosh HD:Developer:About Xcode.pdf
is what I don't want. I only want:
About Xcode
The below answer by Kassym Dorsel doesn't work when there is more than one . in it.
The below answer by Lri doesn't work with set x to choose file:
error "Can’t make quoted form of alias \"Macintosh HD:Applications:Firefox.app:\" into type Unicode text." number -1700 from quoted form of alias "Macintosh HD:Applications:Firefox.app:" to Unicode text
You can use the Finder to manipulate the names of Finder items:
choose file with prompt "Pick one"
set filepath to result
tell application "Finder" to set {dispName, nameExt, isHidden} to ¬
the {displayed name, name extension, extension hidden} of the filepath
if isHidden or nameExt is equal to "" then
dispName
else
(characters 1 through (-2 - (count of nameExt)) of dispName) as text
end if
set baseName to result
This will work :
set a to "Macintosh HD:Developer:About.Xcode.pdf"
set text item delimiters to ":"
set temp to last text item of a
set text item delimiters to "."
set temp to text items 1 thru -2 of temp as text
Gives => About.Xcode