Applescript application read from file - file-io

I have a compiled AppleScript application which I have moved to my windows server. I'd like to then insert a text file into the application (which looks like a zip file on windows):
myapplescript.app/Contents/Resources/MyNewDir/MyTxtFile.txt
So, I've precompiled the AppleScript to try to read from this text file and get the contents as a string. This is what I do:
set theFolder to POSIX path of (the path to me)
set theFile to theFolder & "Contents/Resources/MyNewDir/MyTxtFile.txt"
open for access theFile
set fileContents to (read theFile)
close access theFile
but this is the error I get:
Can't make
"/Users/mike/Desktop/myapplescript.app/Contents/Resources/MyNewDir/MyTxtFile.txt"
into type file

Ok, I figured it out, I changed the second line to this:
set theFile to (POSIX file (theFolder & "Contents/Resources/MyNewDir/MyTxtFile.txt"))

There is also a single line version of read:
read POSIX file "/tmp/test.txt" as «class utf8»
Both versions use MacRoman unless you add as «class utf8». (as Unicode text is UTF-16.)

Reading a file via a file path in a variable.
The 1st two work. The 3rd, which stores the file name in variable does not.
set myData to read file POSIX file ¬
"/Users/sww/Devel/afile.csv"
set myData to read file ¬
"Macintosh HD:Users:sww:Devel:afile.csv"
set fRef to "Macintosh HD:Users:sww:Devel:afile.csv"
set myData to read file fRef -- No good
To fix? Give the file reference as a string.
set myData to read file (fRef as string) -- OK

Related

Missing txt file from Visual Basic

I am working in Visual Basic 2017. I have tried to add the file to the Debug folder, but then it just shows that the txt file ienter image description heres missing. I don't have the option under the "Word Solution".. How can I make the file show up? It keeps telling me it doesn't exist.
Dim inFile As IO.StreamReader
Const FileName As String = "words.txt"
Dim subscript As Integer
You can get the path of the directory (Debug or Release or any other) of the *.exe file with:
Dim directory as String = My.Application.Info.DirectoryPath
Using this information, you can then construct the full path with
Dim path As String = IO.Path.Combine(directory, FileName)
If IO.File.Exists(path) Then
...
You can check in Windows File Explorer to see where the file actually is (notice the Copy Path on the ribbon). In File Explorer you will see that the .exe you are running is down 2 directories from the Words Project directory. The double dots in the path is an old DOS way to navigating around directories without having to type out the whole path. This tells the compiler to find the file up 2 directories from the current directory.
For testing purposes this will work. For a release version you could add the file to Resources and access it the same way in any version.
You don't need a stream for a text file. .ReadAllLines returns an array of the lines in the text file
Private Sub OpCode()
Dim words = File.ReadAllLines("..\..\words.txt")
End Sub

Using AppleScript to programmatically create an AppleScript file in plain text format

I have an AppleScript that is used to programmatically create a test script file in one of these Office 2016 app folders:
~/Library/Application Scripts/com.microsoft.Excel
~/Library/Application Scripts/com.microsoft.Word
~/Library/Application Scripts/com.microsoft.Powerpoint
This is the test.scpt file content which is programmatically generated:
on handlerTest(thisPhrase)
say thisPhrase
end handlerTest
This test.scpt file contains a single handler which speaks the phrase passed to it.
When the script is created in one of these folders, I cannot see the content of the script file in Finder and calling the handler from a Microsoft Office app using the new VBA AppleScriptTask causes the Office app to crash. I think the script is being created as a byte-compiled file because it cannot be viewed in Finder as plain text.
If I then copy the script file generated programmatically by my script creator script to the Documents folder, the plain-text content of the script is viewable in Finder.
Now, if I copy the script file from the Documents folder back to the corresponding com.microsoft folder (without modifying it), I can now see the plain-text content in Finder and calling the handler using the VBA AppleScriptTask function works as expected. I don't understand how the format is apparently changing due to copy/paste actions?
How can I programmatically create the script file in the com.microsoft.xyz folder in plain text format?
Here is my VBA procedure:
Sub TestScript()
AppleScriptTask "test.scpt", "handlerTest", "hello world"
End Sub
Here is my example script creator script which programmatically creates a test.scpt file in the com.microsoft.Powerpoint scripting folder: (kudos to eliteproxy for the original source script)
property theFolders : {"~/Library/'Application Scripts'/com.microsoft.Powerpoint"}
try
tell application "Finder" to set targetFolder to (target of the front window) as alias
on error -- no window
set targetFolder to (choose folder)
end try
# build a parameter string from the folder list
set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}
set {theFolders, AppleScript's text item delimiters} to {theFolders as text, tempTID}
do shell script "cd " & quoted form of POSIX path of targetFolder & "; mkdir -p " & theFolders
--Write the Script file if it does not exist
if ExistsFile("~/Library/'Application Scripts'/com.microsoft.Powerpoint/test.scpt") is false then
tell application "Finder"
--GET THE WORKING DIRECTORY FOR FILE COPY OF SCRIPT
get folder of (path to me) as Unicode text
set workingDir to POSIX path of result
--Write the new script in the current working directory
set textFile to workingDir & "test.scpt"
--Delete script if it exists
set posixPath to POSIX path of textFile as string
do shell script "rm -rf \"" & posixPath & "\""
--Create Script Interface file for Microsoft PowerPoint VBA Applications
set fd to open for access textFile with write permission
-- Create test handler which speaks the passed phrase parameter
write "on handlerTest(thisPhrase)" & linefeed to fd as «class utf8» starting at eof
write "say thisPhrase" & linefeed to fd as «class utf8» starting at eof
write "end handlerTest" & linefeed to fd as «class utf8» starting at eof
close access fd
--Copy the script file into the MACOS-Specific 'safe' folder
set fromPath to quoted form of POSIX path of (workingDir) & "test.scpt"
set toPath to quoted form of "~/Library/'Application Scripts'/com.microsoft.Powerpoint"
do shell script "cp -R " & fromPath & space & "~/Library/'Application Scripts'/com.microsoft.Powerpoint" with administrator privileges
end tell
end if
--Delete the temp script file from the working directory
set posixPath to POSIX path of textFile as string
do shell script "rm -rf \"" & posixPath & "\""
--Provide confirmation
set theAlertTitle to "TEST"
set theAlertMsg to "The script has been successfully installed."
display alert theAlertTitle message theAlertMsg as informational buttons {"OK"} default button "OK" cancel button "OK"
--For use when checking if a file exists
on ExistsFile(filePath)
tell application "System Events" to return (exists disk item filePath) and class of disk item filePath = file
end ExistsFile
I could be wrong in my interpretation of your question, but it appears as if you are looking to create file “Test.scpt” with your handler “handlerTest” as the code, in a folder named “com.microsoft.Excel” (for example). If that is all you are looking to achieve, I believe this solution should work for you...
script theHandler
on handlerTest(thisPhrase)
say thisPhrase
end handlerTest
end script
storeScript()
on storeScript()
set thisScript to store script theHandler in (path to home folder as text) ¬
& "Library:Application Scripts:com.microsoft.Excel:Test.scpt" replacing yes
end storeScript

Applpescript: Reading Text file for List of Files

I have been trying to cobble together a script to take a list of files from a text document and have applescript go through the list line by line and then do something with the file. In this case it is changing the label on the file, but we would use it in other instances to move files, etc.
It works with a test file on my desktop the files get marked with the purple Label, but when trying to run it in the folder I actually need to it fails with this error message:
error "Finder got an error: Can’t set 1 to 5." number -10006 from 1
The text files are the same except for the length of their content.
Could this be a an issues with filenames, and if so how do I make the script more tolerant.
Here is the script:
set textFileContents to POSIX path of (choose file)
set dataList to paragraphs of (read textFileContents as «class utf8»)
tell application "System Events"
repeat with thisFileName in dataList
tell application "Finder" to set (label index of files whose name is thisFileName) to 5
end repeat
end tell
Any help would be appreciated, thanks.
1080074 3.tif
1080074 2.tif
1080069_A1.tif
Here is the final code from the solution to this problem and some further work I did.
Thanks to #Mark Setchell & #jackjr300 for all of their patient help.
set justpath to POSIX path of (choose folder with prompt "Select the Folder with Files You Want to Use")
set textFileContents to (choose file with prompt "Select the list of files")
set dataList to paragraphs of (read textFileContents as «class utf8»)
tell application "Finder"
repeat with FileName in dataList
try -- need a try block to ignore error, in case that the file has been moved or deleted
set label index of (justpath & FileName as POSIX file as alias) to 5
end try
end repeat
end tell
You seem to have a spurious tell application "System Events" in there. It works like this:
set FileName to POSIX path of (choose file)
set FileRecords to paragraphs of (read FileName)
repeat with ThisFileName in FileRecords
say ThisFileName
tell application "Finder" to set (label index of files whose name is thisFileName) to 5
end repeat
Note that my test file isn't UTF8.
Update
By the way, if all you want do is set the label colour on some files, it may be easier to do that from the Terminal and not worry with Applescript. Say you start the Terminal, and go to your Desktop like this
cd Desktop
you can then change the labels of all files on your Desktop (and in any subdirectories) whose names contain "Freddy" followed by "Frog" (i.e "fileForFreddyFrog.txt", "file from Freddy the Frog.php")
find . -name "*Freddy*Frog*" -exec xattr -wx com.apple.FinderInfo "0000000000000000000700000000000000000000000000000000000000000000" {} \;
You need to specify the folder because the finder has no current folder, except the Desktop if you don't specify a folder.
set textFileContents to choose file
set dataList to paragraphs of (read textFileContents as «class utf8»)
set theFolder to "/Users/jack/Desktop/xxx/yyy" as POSIX file as alias -- change it to the path of your folder
tell application "Finder"
repeat with thisFileName in dataList
try -- need a try block to ignore error, in case that the file has been moved or deleted
set label index of file thisFileName of theFolder to 5
end try
end repeat
end tell
Change the path in the third line of this script
--
If the text file contains the full path of the file, you can use this script.
set textFileContents to choose file
set dataList to paragraphs of (read textFileContents as «class utf8»)
tell application "Finder"
repeat with thisPath in dataList
try -- need a try block to ignore error, in case that the file has been moved or deleted
set label index of (thisPath as POSIX file as alias) to 5
end try
end repeat
end tell

How do I specify the file path in vba code in powerpoint 2011 for mac?

I want to access an image file in my VBA code for PowerPoint 2011 for Mac.
I have used both "/" and ":" as separator but it didn't work.
It works only when I save both .pptm file and the image file in the same folder and specify the relative path for the image file as below:
Dim strPath As String
strPath = "image.png"
But if I try saving the file at a different location than the .pptm file and provide the full path for the StrPath as:
strPath = "Users:Me:Desktop:image.png"
it throws a runtime error saying "the specified file wasn't found".
I have kept in mind the case sensitivity in case of Mac but nothing seems to be working for me.
Can anyone please help me in finding the possible workaround so that I can save the .pptm file and the image file at different locations on Mac?
Try:
Presentations.Open "Macintosh HD:Users:Me:Desktop:filename.ext"
Substitute appropriate strings for the name of the HD, your user name and the file.

Open a text file with vb.net , and if it exists delete that file

I am writing code for a windows application using vb.net. I want to open a text file under c:\. If the file already exists I want to delete that file.
my code
-------
Dim file As String = "C:\test.txt"
If System.IO.File.Exists(file) Then
file.Remove(file)
Else
System.Diagnostics.Process.Start(file)
End If
I am getting the following error when I try to open that file.
error
-----
The system cannot find the file specified
Apart from Konrad's point about trying to execute a file that you have just checked does not exist:
1) It's not a good idea to name your variable file as it could get confused with System.IO.File.
2) It's File.Delete, not file.Remove - you're calling the String.Remove method because file is a string. You should use Option Strict On because it would have caught that error for you.
3) On Windows Vista and later, you may not have write/delete access to C:.
Assuming you have write access to the directory C:\temp then this works:
Dim fyle As String = "C:\temp\test.txt"
If System.IO.File.Exists(fyle) Then
IO.File.Delete(fyle)
End If
IO.File.Create(fyle)
System.Diagnostics.Process.Start(fyle)