I wanna to to create and open Text file via Applescript - applescript

For now I can generate the text file with applescript with this code
tell application "Finder"
try
set fileName to "untitled"
if length of fileName = 0 then
return 0
end if
set fileExt to ".txt"
set thisFolder to the target of the front window as alias
set newFile to fileName & fileExt
make new file at thisFolder with properties {name:newFile, file type:"TEXT", creator type:"ttxt"}
on error errMsg
display dialog (errMsg)
end try
end tell
but I want to open this new file that just created by the script instantly
May anyone suggest that ?

To open the newly created text file immediately, you could change:
make new file at thisFolder with properties {name:newFile, file type:"TEXT", creator type:"ttxt"}
To:
open (make new file at thisFolder with properties {name:newFile, file type:"TEXT", creator type:"ttxt"})
This will immediately open the newly created text file in the application set as the default to handle this type of file. By default, if it hasn't been changed, it will be opened in TextEdit.

Related

AppleScript : How to get files in folder without hidden files?

I actually have two questions.
How to exclude hidden files like .DS_STORE, Icon when I try to get files in folder ?
I've tried "without invisibles" but it seems not working.
How to set my var the_new_folder as an existing folder if already exists ?
Thanks for answers.
My code:
--
-- Get all files in a selected folder
-- For each file, create a folder with the same name and put the file in
--
tell application "Finder"
set the_path to choose folder with prompt "Choose your folder..."
my file_to_folder(the_path)
end tell
on file_to_folder(the_folder)
tell application "Finder"
-- HELP NEEDED HERE
-- HOW TO EXCLUDE HIDDEN FILES (Like Icon, .DS_STORE, etc)
set the_files to files of the_folder
repeat with the_file in the_files
-- Exclude folder in selection
if kind of the_file is not "Folder" then
set the_path to container of the_file
set the_file_ext to name extension of the_file
-- Remove extension of the file name
set the_file_name to name of the_file as string
set the_file_name to text 1 thru ((offset of the_file_ext in (the_file_name)) - 2) of the_file_name
-- Make the new folder with the file name
try
set the_new_folder to make new folder at the_path with properties {name:the_file_name}
on error
-- HELP NEEDED HERE
-- HOW TO SET the_new_folder AS THE EXISTING FOLDER
end try
-- Move the file in the new folder
move the_file to the_new_folder
end if
end repeat
end tell
end file_to_folder
tell application "Finder"
(display dialog ("It's done!") buttons {"Perfect!"})
end tell
Using the System Events context instead of Finder:
bypasses the problem with the AppleShowAllFiles preference[1]
is much faster in general.
Using the visible property of file / folder objects in the System Events context allows you to predictably determine either all items, including hidden ones (by default), or only the visible ones (with whose visible is true):
# Sample input path.
set the_path to POSIX path of (path to home folder)
tell application "System Events"
set allVisibleFiles to files of folder the_path whose visible is true
end tell
Simply omit whose visible is true to include hidden files too.
The code for either referencing a preexisting folder or creating it on demand is essentially the same as in the Finder context:
# Sample input path.
set the_path to POSIX path of (path to home folder)
# Sample subfolder name
set the_subfolder_name to "subfolder"
tell application "System Events"
if folder (the_path & the_subfolder_name) exists then
set subfolder to folder (the_path & the_subfolder_name)
else
set subfolder to make new folder at folder the_path ¬
with properties {name: the_subfolder_name}
end if
end tell
[1] In order to predictably exclude hidden items, a Finder-based solution is not only cumbersome but has massive side effects:
You need to determine the current state of the the AppleShowAllFiles preference (defaults read com.apple.Finder AppleShowAllFiles),
then turn it off.
then kill Finder to make the change take effect (it restarts automatically) - this will be visually disruptive
then, after your code has run, restore it to its previous value.
then kill Finder again so that the restored value takes effect again.
I believe that your first question is not a problem. Your code works fine for me.
As for the second issue, the simplest method is to use the text representation of the_path, and simply build the new folder, and see if it already exists:
set the_path_Text to (the_path as text)
set try_Path to the_path_Text & the_file_name
if (folder try_Path) exists then
set the_new_folder to (folder try_Path)
else
set the_new_folder to make new folder at the_path with properties {name:the_file_name}
end if
If you are truly having difficulty with the first code section, please post a new question with more details, such as a copy of the Result section of the Script.
Thank you to all of you ! #mklement0 #craig-smith
You will find below the corrected code with your help!
If I share this code, you want to be cited for thanks?
--
-- Get all files in a selected folder
-- For each file, create a folder with the same name and put the file in
--
-- Get user folder
set the_path to choose folder with prompt "Choose your folder..."
my file_to_folder(the_path)
on file_to_folder(the_folder)
tell application "System Events"
-- Get all files without hidden
set the_files to files of the_folder whose visible is true
repeat with the_file in the_files
-- Remove extension of the file name
set the_file_ext to name extension of the_file
set the_file_name to name of the_file as string
set the_file_name to text 1 thru ((offset of the_file_ext in (the_file_name)) - 2) of the_file_name
-- Make a new folder or get the existing
set the_path to POSIX path of the_folder
if folder (the_path & the_file_name) exists then
set the_new_folder to folder (the_path & the_file_name)
else
set the_new_folder to make new folder at folder the_path with properties {name:the_file_name}
end if
-- Move the file to the new folder
move the_file to the_new_folder
end repeat
end tell
end file_to_folder
-- Ending dialog
display dialog ("It's done!") buttons {"Perfect!"}

AppleScript: Garbage values written to file when using path to command

I am trying to write a simple applescript that organises the files in any selected folder. I want to make the script such that it runs at specific intervals and re-organizes the folder if something has changed. For this, I am trying to save the path of the user chosen folder to a file. Each time the script runs, it reads the folder path from this file.
here's a snippet from the code:
set home_path to get path to home folder
tell application "Finder"
set home_folder to folder (home_path as string)
if not (exists file "Clfd_config.cf1" in home_folder) then
set (folder_path) to choose folder with prompt "Choose the folder to organize"
set this_folder to folder (folder_path as string)
set path_file to open for access file (home_path & "Clfd_config.cf1" as text) with write permission
write folder_path to path_file
close access path_file
else
set path_file to open for access file (home_path & "Clfd_config.cf1" as string)
set folder_path to read path_file as string
set this_folder to folder (folder_path as string)
close access path_file
end if
end tell
However, when I open the file, it has garbled information, like so:
������Harshad��������������������œ‘xH+��� 7 Desktop����������������������������������������� ���������������� 7Éœ‘zç��������ˇˇˇˇ��I ���������� ������œ‘*∆������œ‘-5������D�e�s�k�t�o�p��� �H�a�r�s�h�a�d��Users/harshad/Desktop���/����ˇˇ������
When I try to read this file n the script, the script obviously fails.
I have tried telling the script to write the file as string, as text, but I keep getting the error that the folder_path variable cannot be converted to text or string.
What should I do so that the path is saved properly and the script can read it back from the saved file?
The main issue is that you're writing an alias file specifier to disk rather than a string path.
I added basic error handling while writing to disk and removed some redundant code
property configFileName : "Clfd_config.cf1"
tell application "Finder"
set configFile to (home as text) & configFileName
if not (exists file configFile) then
set folder_path to choose folder with prompt "Choose the folder to organize"
try
set fileReference to open for access file configFile with write permission
write (folder_path as text) to fileReference
close access fileReference
on error
try
close access file configFile
end try
end try
else
set folder_path to read file configFile
set this_folder to folder folder_path
end if
end tell
It seems you just want to save value of a folder path and be able to find it again during next run.
if only that, why are you using subroutine to write this path to a text file at specific place ? isn't it easier to you use the characteristics of "property" objects ?
"Property" variable can be changed and it keeps new value in the script itself, until next compilation.
Try this script bellow : the first run, it will ask you for folder. Select it. Any next run, it will just display the folder selected during first run !
property My_Folder : ""
if My_Folder is "" then -- first run, ask user to select folder
Set My_Folder to (choose folder with prompt "choose the folder to organise") as string
end if
display dialog "folder selected = " & My_Folder
it does the same thing than read/write from text file...

applescript help naming .plist and removing extention

i am creating an applescript to create a file with property list elements but without the .plist extension!
my issue is if i use a dialog to get the name of the file eg.
tell application "SystemUIServer"
display dialog "Enter filename :- " buttons {"Generate file"} default answer "Generate Keyfile"
set fileName to text returned of result
and create the file on the desktop like so
set text_file to (path to desktop)'s POSIX path & "" & quoted form of fileName & ".NEWextention"
finally i add elements to the .plist
tell application "System Events"
tell (make new property list file with properties {name:text_file})
make new property list item at end with properties {kind:string, name:"regName", value:"FOO"}
make new property list item at end with properties {kind:string, name:"regNumber", value:"BAR" as text}
end tell
end tell
end tell
however the file that is created is has '' when quoted from fileName and still has the .plist extention.
eg input :- myfile
output:- 'myfile'.NEWextention.plist
when i want
myfile.NEWextention
how can i achieve this in applescript?
any help would be greatly appreciated!
many thanks in advance.
below is the fixed code thanks to #McUsr
without his help i would have been at the same point for over 6 months of trial and error
tell application "SystemUIServer"
display dialog "Enter FileName :- " buttons {"Generate file"} default answer "Generate file"
set FileName to text returned of result
set text_file to (path to desktop folder as text) & FileName & ".plist"
set dateStamp to do shell script "date"
tell application "System Events"
tell (make new property list file with properties {name:text_file})
make new property list item at end with properties {kind:string, name:"Name", value:FileName}
make new property list item at end with properties {kind:string, name:"Date", value:dateStamp}
end tell
end tell
end tell
tell application "Finder"
set name extension of file text_file to "newExt"
end tell
You can make it happen afterwards you are done processing it with System Events, by Finder, (set name extension of file "Hfs:path:to:file:with:name.ext" to "new-ext").
But I am not sure if you can expect System Events to regard the file as a property list file, containing property list items afterwards. It is still worth a try though. :)
This is how you must change the name extension, as this don't work with System Events (name extension is a read only property).
tell application "Finder"
set mf to (path to desktop folder as text) & "labels.txt"
set name extension of file mf to "text"
end tell
Use the HFS path of the file, aka: Macintosh Hd:Users:You:path:file.ext, and not the posix path. And don't use quoted form of det path.

capturing edited file name from duplicated file

I copy a a file to a new folder then rename it. Next I want to open it:-
set myNewFile to duplicate myFile to myNewLocation
set the name of myNewFile to myNewFileName
open myNewFile
The first 2 lines above work but the open command doesn't work because it can't find the file since I renamed it. I want to do something like:-
open myNewLocation & myNewFileName
myNewLocation is a valid path, myNewFileName is just a string, I have all the data I need but I can't work out how to construct a valid usable path from the two items.
Alternatively I could maybe change the name at the same time as I duplicate the file, before I save it to myNewFile. I've tried:-
set myNewFile to duplicate myFile to myNewLocation with name myNewFileName
and
set myNewFile to duplicate myFile to myNewLocation with properties {name:myNewFileName}
Neither works.
Try:
tell application "Finder" to set myFile to file "Mac OS X:Users:JD:Desktop:testStart.txt"
set myNewLocation to (path to desktop as text)
set myNewFileName to "testEnd.txt"
set myNewFile to duplicate myFile to myNewLocation
set the name of myNewFile to myNewFileName
tell application "Finder" to open file (myNewLocation & myNewFileName)

Automator/Applescript File Sorting and Moving

I was wondering if there was a way in Automator to use Applescript code to get the extension of a file, and if it equals a specific extension (e.g. .pdf or .rtf) move to a specific folder for that extension (e.g if (extension == pdf) { move to folder "~/PDF Files" } else if (extension == rtf) { move to folder "~/Rich Text Files" })
Here's an applescript. Since your request was simple I just wrote it for you. Note how I get the file extension with the subroutine "getNameAndExtension(F)". Normally you can get the file extension from the Finder (called name extension) but I've found that the Finder is not always reliable so I always use that subroutine. That subroutine has always been reliable.
set homeFolder to path to home folder as text
set rtfFolderName to "Rich Text Files"
set pdfFolderName to "PDF Files"
-- choose the files
set theFiles to choose file with prompt "Choose RTF or PDF files to move into your home folder" with multiple selections allowed
-- make sure the folders exist
tell application "Finder"
if not (exists folder (homeFolder & rtfFolderName)) then
make new folder at folder homeFolder with properties {name:rtfFolderName}
end if
if not (exists folder (homeFolder & pdfFolderName)) then
make new folder at folder homeFolder with properties {name:pdfFolderName}
end if
end tell
-- move the files
repeat with aFile in theFiles
set fileExtension to item 2 of getNameAndExtension(aFile)
if fileExtension is "rtf" then
tell application "Finder"
move aFile to folder (homeFolder & rtfFolderName)
end tell
else if fileExtension is "pdf" then
tell application "Finder"
move aFile to folder (homeFolder & pdfFolderName)
end tell
end if
end repeat
(*=============== SUBROUTINES ===============*)
on getNameAndExtension(F)
set F to F as Unicode text
set {name:Nm, name extension:Ex} to info for file F without size
if Ex is missing value then set Ex to ""
if Ex is not "" then
set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
end if
return {Nm, Ex}
end getNameAndExtension
I'm not at my Apple right now, so I can't play around with Automator and figure it out. But if you could do this by switching finder to list view, sort by type, and then select blocks of files of the same type and drag them into the right folder.

Resources